Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Quicksort Algorithm

Uploaded by

ahmedaboamod08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Quicksort Algorithm

Uploaded by

ahmedaboamod08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lecture 8: Divide and Conquer (Quicksort Algorithm)

By: Milad Elgargni

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.

Working principal of Quicksort Algorithm


There are different variations of quicksort where the pivot element is selected from different positions.
Here, we will be selecting the rightmost element of the array as the pivot element.
Consider the following three forints of array.
10 55 60 20 12 78 19 the number 10 is already in sorted position.
13 55 7 8 34 23 45 90 the number 90 is already in sorted position.
1 4 8 6 3 10 11 66 84 the number 10 is already in sorted position.
 Step 1 is to select the Pivot Element.

Figure 1: The steps of quick sort

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.

Here's the description of the above procedure.


1. A pointer is fixed at the pivot element. The pivot element is compared with the elements beginning
from the first index.
2. If the element is greater than the pivot element, a second pointer is set for that element.
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.
5. The process goes on until the second last element is reached.
6. Finally, the pivot element is swapped with the second pointer.
Pivot elements are again chosen for the left and the right sub-parts separately.
 Step 2 is to apply divide and conquer approach.

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

Quick sort algorithm in c++


int partition(int arr[], int l, int h)
{
int pivot = arr[h];
int i = l -1;
int j = l;
while (j < h)
{
if (arr[j] < pivot)
{i = i + 1 ;
swap(arr[i], arr[j]);
}
j++; }
swap(arr[i+1], arr[h]);
return i+1;
}
quicksort(l, h)
{
if (l < h)
{
pivot = partition(l,h);
quicksort ( l, pivot - 1);
quicksort (pivot + 1, h);
}
}

You might also like