Sorting and Serching Coding
Sorting and Serching Coding
Approach:
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.
Approach:
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.
Approach:
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