Quick Sort & Merge Sort
Quick Sort & Merge Sort
Quicksort is a sorting algorithm based on the divide and conquer approach where
1. An array is divided into subarrays by selecting a pivot element (element selected
from the array).
While dividing the array, the pivot element should be positioned in such a way that
elements less than pivot are kept on the left side and elements greater than pivot
are on the right side of the pivot.
2. The left and right subarrays are also divided using the same approach. This
process continues until each subarray contains a single element.
3. At this point, elements are already sorted. Finally, elements are combined to form
a sorted array.
1. A pointer is fixed at the pivot element. The pivot element is compared with the
elements beginning from the first index.
Comparison of pivot
element with element beginning from the first index
2. If the element is greater than the pivot element, a second pointer is set for that
3. Now, pivot is compared with other elements. If an element smaller than the pivot
element is reached, the smaller element is swapped with the greater element
found earlier. Pivot is
compared with other elements.
4. Again, the process is repeated to set the next greater element as the second
pointer. And, swap it with another smaller element.
3. Divide Subarrays
Pivot elements are again chosen for the left and the right sub-parts separately. And, step
2 is repeated.
Experiment No:3
Write a program to sort given set of numbers in ascending/descending order using Quick sort and any
other sorting algorithm. Also record the time taken by these two programs and compare them.
To study and Implement Sort Algorithm
Knowledge of
• Array Data Structure
• Divide and Conquer Technique
Quick Sort Algorithm:
#include<stdio.h>
int x, y, p, temp;
if(low<high){
p=low;
x=low;
y=high;
while(x<y){
while(array[x]<=array[p]&&x<high)
x++;
while(array[y]>array[p])
y--;
if(x<y){
temp=array[x];
array[x]=array[y];
array[y]=temp;
temp=array[p];
array[p]=array[y];
array[y]=temp;
quicksort(array,low,y-1);
quicksort(array,y+1,high);
}
int main(){
printf("How many elements should the array have? (Max. - 25): ");
scanf("%d",&count);
for(x=0;x<count;x++)
scanf("%d",&array[x]);
quicksort(array,0,count-1);
for(x=0;x<count;x++)
printf(" %d",array[x]);
return 0;
}
Merge Sort
If we can break a single big problem into smaller sub-problems, solve the
smaller sub-problems and combine their solutions to find the solution for the
original big problem, it becomes easier to solve the whole problem.
When Britishers came to India, they saw a country with different religions living
in harmony, hard working but naive citizens, unity in diversity, and found it
difficult to establish their empire. So, they adopted the policy of Divide and
Rule. Where the population of India was collectively a one big problem for
them, they divided the problem into smaller problems, by instigating rivalries
between local kings, making them stand against each other, and this worked
very well for them.
Well that was history, and a socio-political policy (Divide and Rule), but the
idea here is, if we can somehow divide a problem into smaller sub-problems, it
becomes easier to eventually solve the whole problem.
2. Conquer the subproblems by solving them. The idea is to break down the
problem into atomic subproblems, where they are actually solved.
In merge sort, we break the given array midway, for example if the original array
had 6 elements, then merge sort will break it down into two subarrays
with 3 elements each.
But breaking the orignal array into 2 smaller subarrays is not helping us in
sorting the array.
So we will break these subarrays into even smaller subarrays, until we have
multiple subarrays with single element in them. Now, the idea here is that an
array with a single element is already sorted, so once we break the original array
into subarrays which has only a single element, we have successfully broken
down our problem into base problems.
And then we have to merge all these sorted subarrays, step by step to form one
single sorted array.
Below, we have a pictorial representation of how merge sort will sort the given
array.
In merge sort we follow the following steps:
1. We take a variable p and store the starting index of our array in this. And
we take another variable r and store the last index of array in it.
2. Then we find the middle of the array using the formula (p + r)/2 and mark
the middle index as q, and break the array into two subarrays,
from p to q and from q + 1 to r index.
3. Then we divide these 2 subarrays again, just like we divided our main array
and this continues.
4. Once we have divided the main array into subarrays with single elements,
then we start merging the subarrays.
mid = (left+right)/2
end
#include <stdio.h>
// lets take a[5] = {32, 45, 67, 2, 7} as the array to be
sorted.
int q;
if(p < r)
q = (p + r) / 2;
mergeSort(a, p, q);
merge(a, p, q, r);
int i, j, k;
k = 0;
i = p;
j = q + 1;
else
b[k++] = a[j++];
while(i <= q)
b[k++] = a[i++];
while(j <= r)
b[k++] = a[j++];
}
for(i=r; i >= p; i--)
int i;
printf("\n");
int main()
printArray(arr, len);
printArray(arr, len);
return 0;