Quick Sort
Quicksort is a sorting algorithm based on the divide and conquer approach where
ď‚· 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.
ď‚· The left and right subarrays are also divided using the same approach. This process
continues until each subarray contains a single element.
ď‚· At this point, elements are already sorted. Finally, elements are combined to form a sorted
array.
Example:
0 1 2 3 4 5 6 7
50=i 30 10 90 80 20 40 70=j
Pivot=50
0 1 2 3 4 5 6 7
50 30 10 90=i 80 20 40=j 70
0 1 2 3 4 5 6 7
50 30 10 40=i 80 20 90=j 70
0 1 2 3 4 5 6 7
50 30 10 40 80=i 20=j 90 70
0 1 2 3 4 5 6 7
50 30 10 40 20=i 80=j 90 70
0 1 2 3 4 5 6 7
50 30 10 40 20=j 80=i 90 70
0 1 2 3 4 5 6 7
20 30 10 40 50 80 90 70
Left Half
0 1 2 3
20=i 30 10 40=j
Pivot=20
0 1 2 3
20 30=i 10=j 40
0 1 2 3
20 10=i 30=j 40
0 1 2 3
20 10=j 30=i 40
0 1 2 3
10 20 30 40
Right Half
5 6 7
80=i 90 70=j
Pivot=80
5 6 7
80 90=i 70=j
5 6 7
80 70=i 90=j
5 6 7
80 70=j 90=i
5 6 7
70 80 90
Finally
0 1 2 3 4 5 6 7
10 20 30 40 50 70 80 90
Algorithm QuickSort(l,h)
{
if(l<h)
{
j=Partition(l,h);
QuickSort(l,j);
QuickSort(j+1,h);
}
}
Algorithm Partition(l,h)
{
Pivot=A[l];
i=l,j=h;
while(i<j)
{
do
{
i++;
}while(A[i]<=pivot);
do
{
j--;
}while(A[j]>pivot);
if(i<j)
swap(A[i],A[j]);
}
Swap(A[l],A[j]);
return j;
}

Quick sort

  • 1.
    Quick Sort Quicksort isa sorting algorithm based on the divide and conquer approach where ď‚· 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. ď‚· The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element. ď‚· At this point, elements are already sorted. Finally, elements are combined to form a sorted array. Example: 0 1 2 3 4 5 6 7 50=i 30 10 90 80 20 40 70=j Pivot=50 0 1 2 3 4 5 6 7 50 30 10 90=i 80 20 40=j 70 0 1 2 3 4 5 6 7 50 30 10 40=i 80 20 90=j 70 0 1 2 3 4 5 6 7 50 30 10 40 80=i 20=j 90 70 0 1 2 3 4 5 6 7 50 30 10 40 20=i 80=j 90 70 0 1 2 3 4 5 6 7 50 30 10 40 20=j 80=i 90 70 0 1 2 3 4 5 6 7 20 30 10 40 50 80 90 70
  • 2.
    Left Half 0 12 3 20=i 30 10 40=j Pivot=20 0 1 2 3 20 30=i 10=j 40 0 1 2 3 20 10=i 30=j 40 0 1 2 3 20 10=j 30=i 40 0 1 2 3 10 20 30 40 Right Half 5 6 7 80=i 90 70=j Pivot=80 5 6 7 80 90=i 70=j 5 6 7 80 70=i 90=j
  • 3.
    5 6 7 8070=j 90=i 5 6 7 70 80 90 Finally 0 1 2 3 4 5 6 7 10 20 30 40 50 70 80 90 Algorithm QuickSort(l,h) { if(l<h) { j=Partition(l,h); QuickSort(l,j); QuickSort(j+1,h); } } Algorithm Partition(l,h) { Pivot=A[l]; i=l,j=h; while(i<j) { do { i++; }while(A[i]<=pivot); do {
  • 4.