Quick Sort
Quick Sort
Quicksort uses divide-and-conquer, and so it's a recursive
algorithm.
Divide by choosing any element in the array[0….n-1].
Call this element the pivot.
Rearrange the elements in array so that all elements in array that
are less than or equal to the pivot are to its left and
all elements that are greater than the pivot are to its right.
We call this procedure partitioning.
At this point, it doesn't matter what order the elements to the left
of the pivot are in relation to each other, and the same holds for
the elements to the right of the pivot.
We just care that each element is somewhere on the correct side
of the pivot
Quick Sort
Conquer by recursively sorting the subarrays.
That is(all elements to the left of the pivot, which must be less than or
equal to the pivot) and (all elements to the right of the pivot, which must
be greater than the pivot).
Procedur[Link]Quick Sort
• 1. Choose starting element as PIVOT (0th element)
• 2. Initialize i to low index and j to high index.(i=0 and j=n-1)
• 3. Repeat the following procedure until i less than j
– 3.1 Keep on increment i value until element at position i less than or equal to
PIVOT
– 3.2 Keep on decrement j value until element at position j greater than PIVOT
– 3.3 if i less than j then swap element at position i & element at position j
• 4. if i greater than j then swap element at position j & PIVOT
• Finally j position is the PIVOT position so that elements left to PIVOT are
less than PIVOT value and elements right to PIVOT are greater than PIVOT.
• Do the same process for two partitions
– partition1 ---- low to PIVOT-1
– partition2 ---- PIVOT+1 to high
10 16 8 12 15 6 3 9 5 20
A[i]<=pivot 10<=10 16<=10
i=0 i=1 false
i++
A[j]>pivot 20>10 5>10
J=9 J=8 false
J--
If i< j
swap( a[i],a[j])-- swap(16,5)
else
swap(a[j], pivot)
10 5 8 12 15 6 3 9 16 20
10 5 8 12 15 6 3 9 16 20
A[i]<=pivot 5<=10 8<=10 12<=10
i=1 i=2 i=3 false
i++
A[j]>pivot 16>10 9>10
J=8 J=7 false
J--
If i< j
swap( a[i],a[j])----swap 12 &9
else
swap(a[j], pivot)
10 5 8 9 15 6 3 12 16 20
0 1 2 3 4 5 6 7 8 9
10 5 8 9 15 6 3 12 16 20
A[i]<=pivot 9<=10 15<=10
i++ (i=3) i=4 false
A[j]>pivot 12>10 3>10
J-- J=6 false
(j=7)
If i< j
swap( a[i],a[j])- swap 15&3
else
swap(a[j], pivot)
10 5 8 9 3 6 15 12 16 20
10 5 8 9 3 6 15 12 16 20
A[i]<=pivot 3<=10 6<=10 15<=10
(i=4) i=5 i=6 false
i++
A[j]>pivot 15>10 6>10
J-- J=5 false
(j=6)
If i< j
swap( a[i],a[j])
else
swap(a[j], pivot)---- swap 6 &10
6 5 8 9 3 10 15 12 16 20
#include <stdio.h>
void quicksort (int [], int, int);
int main()
{ int list[50];
int size, i;
printf("How many elements u want to Sort :: ");
scanf("%d", &size);
printf("\nEnter the elements below to be sorted :: \n");
for (i = 0; i < size; i++)
{
printf("\nEnter [ %d ] element :: ",i+1);
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("\nAfter implementing Quick sort, Sorted List is :: \n\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
void quicksort(int list[], int low, int high)
{ int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] )
{
i++;
}
while (list[j] > list[pivot] )
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}