Sort
Sort
Sort
Example 1:
Input: N = 5 , Arr[] = {4,1,7,9,3}
Output: 1 3 4 7 9
Example 2:
Input: N = 8 , Arr[] = {4,6,2,5,7,9,1,3}
Output: 1 2 3 4 5 6 7 9
Intuition:
Quick Sort is a divide-and-conquer algorithm like the Merge Sort. But unlike Merge sort, this
algorithm does not use any extra array for sorting(though it uses an auxiliary stack space). So, from
that perspective, Quick sort is slightly better than Merge sort.
This algorithm is basically a repetition of two simple steps that are the following:
Pick a pivot and place it in its correct place in the sorted array.
Shift smaller elements(i.e. Smaller than the pivot) on the left of the pivot and larger ones to
the right.
Now, let’s discuss the steps in detail considering the array {4,6,2,5,7,9,1,3}:
Step 1: The first thing is to choose the pivot. A pivot is basically a chosen element of the given
array. The element or the pivot can be chosen by our choice. So, in an array a pivot can be any of the
following:
After choosing the pivot(i.e. the element), we should place it in its correct position(i.e. The place it
should be after the array gets sorted) in the array. For example, if the given array is {4,6,2,5,7,9,1,3},
the correct position of 4 will be the 4th position.
Note: Here in this tutorial, we have chosen the first element as our pivot. You can choose any element
as per your choice.
Step 2: In step 2, we will shift the smaller elements(i.e. Smaller than the pivot) to the left of the pivot
and the larger ones to the right of the pivot. In the example, if the chosen pivot is 4, after performing
step 2 the array will look like: {3, 2, 1, 4, 6, 5, 7, 9}.
From the explanation, we can see that after completing the steps, pivot 4 is in its correct position with
the left and right subarray unsorted. Now we will apply these two steps on the left subarray and
the right subarray recursively. And we will continue this process until the size of the unsorted part
becomes 1(as an array with a single element is always sorted).
So, from the above intuition, we can get a clear idea that we are going to use recursion in this
algorithm.
To summarize, the main intention of this process is to place the pivot, after each recursion call, at its
final position, where the pivot should be in the final sorted array.
Approach:
In the intuition, we have seen that the given array should be broken down into subarrays. But while
implementing, we are not going to break down and create any new arrays. Instead, we will specify the
range of the subarrays using two indices or pointers(i.e. low pointer and high pointer) each time while
breaking down the array.
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.
Pseudocode:
Now, let’s understand how to implement the partition() function to get the partition index.
1. Inside the function, we will first select the pivot(i.e. arr[low] in our case).
2. Now, we will again take two-pointers i and j. The i pointer points to low and the j points to
high.
3. Now, the pointer i will move forward and find the first element that is greater than the pivot.
Similarly, the pointer j will move backward and find the first element that is smaller than the
pivot.
Here, we need to add some checks like i <= high-1 and j >= low+1. Because it might happen
that i is standing at high and trying to proceed or j is standing at low and trying to exceed.
4. Once we find such elements i.e. arr[i] > pivot and arr[j] < pivot, and i < j, we will swap arr[i]
and arr[j].
5. We will continue step 3 and step 4, until j becomes smaller than i.
6. Finally, we will swap the pivot element(i.e. arr[low]) with arr[j] and will return the index j i.e.
the partition index.
Pseudocode:
Note: In the function, we have kept the elements equal to the pivot on the left side. If you choose to
place them on the right, check 1 will be arr[i] < pivot and check 2 will be arr[j] >= pivot.
Output:
Before Using quick Sort:
46257913
After Using quick sort:
12345679
Reason: At each step, we divide the whole array, for that logN and n steps are taken for the partition()
function, so overall time complexity will be N*logN.
The following recurrence relation can be written for Quick sort : F(n) = F(k) + F(n-1-k)
Here k is the number of elements smaller or equal to the pivot and n-1-k denotes elements greater than
the pivot.
It occurs when the pivot element picked is either the greatest or the smallest element.
This condition leads to the case in which the pivot element lies in an extreme end of the sorted array.
One sub-array is always empty and another sub-array contains n - 1 elements. Thus, quicksort is
called only on this sub-array. However, the quicksort algorithm has better performance for scattered
pivots.
This case occurs when the pivot is the greatest or smallest element of the array. If the partition is done
and the last element is the pivot, then the worst case would be either in the increasing order of the
array or in the decreasing order of the array.
It occurs when the pivot element is always the middle element or near to the middle element.
This case occurs when the pivot is the middle element or near to middle element of the array.
Recurrence :
F(n) = 2F(n/2)
Output: 1,2,4,6,7,
Example 2:
Input: N=7,arr[]={3,2,8,5,1,4,23}
Output: 1,2,3,4,5,8,23
Intuition:
1. Merge Sort is a divide and conquers algorithm, it divides the given array into equal parts
and then merges the 2 sorted parts.
2. There are 2 main functions :
merge(): This function is used to merge the 2 halves of the array. It assumes that both parts of
the array are sorted and merges both of them.
mergeSort(): This function divides the array into 2 parts. low to mid and mid+1 to high
where,
3. We recursively split the array, and go from top-down until all sub-arrays size becomes 1.
Approach: