Quicksort Algorithm
Quicksort Algorithm
Quicksort Algorithm
Quicksort is a sorting algorithm based on the divide and conquer approach where
1. An array is divided into sub-arrays 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 sub-arrays are also divided using the same approach. This process continues until
each sub-array contains a single element.
3. At this point, elements are already sorted. Finally, elements are combined to form a sorted array.
1
Lecture 8: Divide and Conquer (Quicksort Algorithm)
By: Milad Elgargni
First select Pivot = 3 then rearrange the Array as shown below in order to put the pivot in the correct
position.
2 6 5 0 8 7 1 3 2 0 5 6 8 7 1 3
i j i j
2 6 5 0 8 7 1 3 2 0 1 6 8 7 5 3
i j i j
2 6 5 0 8 7 1 3 2 0 1 6 8 7 5 3
i j i j
2 0 5 6 8 7 1 3 2 0 1 3 8 7 5 6
i j i j
2 0 5 6 8 7 1 3
i j
Figure 2: Shows the procedure of the quick sort algorithm
Now the pivot is in a correct position and all elements of the array are rearranged in such way were all
elements smaller than the pivot are putted on the left and the elements greater than the pivot are putted on
the right.
2
Lecture 8: Divide and Conquer (Quicksort Algorithm)
By: Milad Elgargni
2 0 1 3 8 7 5 6
i j
2 0 1 3 8 7 5 6
i j i j
0 2 1 3 8 7 5 6
i j i j
0 1 2 3 5 7 8 6
ij i j
3 5 6 8 7
i j
3 5 6 8 7
ij
Figure 3: illustrated applying divide and conquer algorithm