0% found this document useful (0 votes)
14 views

Sorting and Serching Coding

Uploaded by

nigamshreya2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Sorting and Serching Coding

Uploaded by

nigamshreya2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Sor ng

Selection Sort Algorithm


Problem Statement: Given an array of N integers, write a program to implement the
Selection sorting algorithm.

Approach:

The algorithm steps are as follows:

1. First, we will select the range of the unsorted array using a loop (say i)
that indicates the starting index of the range.
The loop will run forward from 0 to n-1. The value i = 0 means the range
is from 0 to n-1, and similarly, i = 1 means the range is from 1 to n-1, and
so on.
(Initially, the range will be the whole array starting from the first index.)
2. Now, in each iteration, we will select the minimum element from the
range of the unsorted array using an inner loop.
3. After that, we will swap the minimum element with the first element of
the selected range(in step 1).
4. Finally, after each iteration, we will find that the array is sorted up to the
first index of the range.

5. for (int i = 0; i < n - 1; i++) {


6. int mini = i;
7. for (int j = i + 1; j < n; j++) {
8. if (arr[j] < arr[mini]) {
9. mini = j;
10. }
11. }
12. //swap
13. int temp = arr[mini];
14. arr[mini] = arr[i];
15. arr[i] = temp;
16. }
Bubble Sort Algorithm
Problem Statement: Given an array of N integers, write a program to implement the
Bubble Sorting algorithm.

Approach:

The algorithm steps are as follows:

1. First, we will select the range of the unsorted array. For that, we will run
a loop(say i) that will signify the last index of the selected range. The loop
will run backward from index n-1 to 0(where n = size of the array). The
value i = n-1 means the range is from 0 to n-1, and similarly, i = n-2
means the range is from 0 to n-2, and so on.
2. Within the loop, we will run another loop(say j, runs from 0 to i-1 though
the range is from 0 to i) to push the maximum element to the last index
of the selected range, by repeatedly swapping adjacent elements.
Basically, we will swap adjacent elements(if arr[j] > arr[j+1]) until the
maximum element of the range reaches the end.
3. Thus, after each iteration, the last part of the array will become sorted.
Like: after the first iteration, the array up to the last index will be sorted,
and after the second iteration, the array up to the second last index will
be sorted, and so on.
4. After (n-1) iteration, the whole array will be sorted.

for (int i = 0; i <n-1; i++) {


for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
Insertion Sort Algorithm
Problem Statement: Given an array of N integers, write a program to implement the
Insertion sorting algorithm.

Approach:

1. Select an element in each iteration from the unsorted array(using a loop).


2. Place it in its corresponding position in the sorted part and shift the
remaining elements accordingly (using an inner loop and swapping).
3. The “inner while loop” basically shifts the elements using swapping.

for (int i = 0; i <= n - 1; i++) {


int j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
j--;
}
}
Merge Sort Algorithm
Problem: Given an array of size n, sort the array using Merge Sort.

Merge func on code like merge two soted sub array


Quick Sort Algorithm
Problem Statement: Given an array of n integers, sort the array using
the Quicksort method.

The algorithm steps are the following for the quickSort() function:

1. Initially, the low points to the first index and the high points to the last
index(as the range is n i.e. the size of the array).
2. After that, we will get the index(where the pivot should be placed after
sorting) while shifting the smaller elements on the left and the larger
ones on the right using a partition() function discussed later.
Now, this index can be called the partition index as it creates a partition
between the left and the right unsorted subarrays.
3. After placing the pivot in the partition index(within the partition() function
specified), we need to call the function quickSort() for the left and the
right subarray recursively. So, the range of the left subarray will be [low
to (partition index – 1)] and the range of the right subarray will be
[(partition index + 1) to high].
4. This is how the recursion will continue until the range becomes 1.
Searching
Best Time Average Time Worst Time Space
Algorithm
Complexity Complexity Complexity Complexity
Bubble Sort O(n) O(n^2) O(n^2) O(1)
Selection
O(n^2) O(n^2) O(n^2) O(1)
Sort
Insertion
O(n) O(n^2) O(n^2) O(1)
Sort
Merge Sort O(n log n) O(n log n) O(n log n) O(n)
Quick Sort O(n log n) O(n log n) O(n^2) O(log n)
Heap Sort O(n log n) O(n log n) O(n log n) O(1)
Counting
O(n + k) O(n + k) O(n + k) O(n + k)
Sort
Radix Sort O(nk) O(nk) O(nk) O(n + k)
Bucket Sort O(n + k) O(n + k) O(n^2) O(n + k)
Binary
O(1) O(log n) O(log n) O(1)
Search
Linear
O(1) O(n) O(n) O(1)
Search

You might also like