Upload
jeanie-arnoco
View
19
Download
0
Embed Size (px)
Citation preview
QUICK SORT
This sorting algorithm uses the idea of divide and conquer.
It finds the element called pivot which divides the array into two halves in such a
way that elements in the left half are smaller than pivot and elements in the
right half are greater than pivot.
QUICK SORT
Three steps
Find pivot that divides the array into two halves.
Quick sort the left half.
Quick sort the right half.
QUICK SORT
Example
Consider an array having 6 elements
5 2 6 1 3 4
Arrange the elements in ascending order using quick sort algorithm
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
Left
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
LeftInitially pointing to the
First element of the array
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
LeftInitially pointing to the
First element of the array
Right
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
LeftInitially pointing to the
First element of the array
RightInitially pointing to the
Last element of the array
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
LeftInitially pointing to the
First element of the array
RightInitially pointing to the
Last element of the array
Pivot
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
LeftInitially pointing to the
First element of the array
RightInitially pointing to the
Last element of the array
Pivot
Initially pointing to theFirst element
0 1 2 3 4 5
5 2 6 1 3 4Array index
Array element
This is our unsorted array
LeftInitially pointing to the
First element of the array
RightInitially pointing to the
Last element of the array
Pivot
Initially pointing to theFirst element
We will quick sort this array
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
All element to the RIGHT of pivot be GREATER than pivot.
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
All element to the RIGHT of pivot be GREATER than pivot.All element to the LEFT of pivot be SMALLER than pivot.
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivotis pointing at
left
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivotis pointing at
left So we will startfrom right
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivotis pointing at
left So we will startfrom right And move towards left
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5Right =4
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5Right =4
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5Right =4NO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5Right =4
So we swap pivot and rightNO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5Right =4
So we swap pivot and rightNO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Right =4
So we swap pivot and rightNO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =4
Is Pivot < Left
Now move pivot to right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =4
Is Pivot < Left
NO
So we swap pivot to the right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivotis pointing at
right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivotis pointing at
rightSo we will start from left
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivotis pointing at
rightSo we will start from left
And move towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =4
Is Pivot > Left( 5 > 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =4
Is Pivot > Left
YES
( 5 > 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =4
Is Pivot > Left
YES
( 5 > 4)
So we move left one position towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =2
Is Pivot > Left( 5 > 2)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =2
Is Pivot > Left( 5 > 2)
YES
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =2
Is Pivot > Left( 5 > 2)
YESSo we move left one position
towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =6
Is Pivot > Left( 5 > 6)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =6
Is Pivot > Left( 5 > 6)
NO
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =6
Is Pivot > Left( 5 > 6)
NO
So we swap pivot and left
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5Left =6
Is Pivot > Left( 5 > 6)
NO
So we swap pivot and left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Left =6
Is Pivot > Left( 5 > 6)
NO
So we swap pivot and left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Left =6
Is Pivot > Left( 5 > 6)
NO
So we swap pivot and left
And move the pivot to left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Now the pivot is pointing at left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Now the pivot is pointing at left So we will start
from right
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Now the pivot is pointing at left So we will start
from right
And move towards left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Right =6
Is Pivot < Right( 5 < 6)
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Right =6
Is Pivot < Right( 5 < 6)
YES
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Right =6
Is Pivot < Right( 5 < 6)
YES
So we move right one position towards left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Right =3
Is Pivot < Right( 5 < 3)
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Right =3
Is Pivot < Right( 5 < 3)
NO
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5Right =3
Is Pivot < Right( 5 < 3)
NOSo we swap pivot and right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5Right =3
Is Pivot < Right( 5 < 3)
NOSo we swap pivot and right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5Right =3
Is Pivot > Right( 5 > 3)
NOSo we swap pivot and right
And move the pivot to right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5Left =3
Is Pivot > Left( 5 > 3)
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5Left =3YES
Is Pivot > Left( 5 > 3)
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5Left =3
Is Pivot > Left( 5 > 3)
So we move left one position towards right
YES
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5Left = 1
Is Pivot > Left( 5 > 1)
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the sorted position
45
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the sorted position
45
Elements left of pivot are smaller
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the sorted position
45
Elements left of pivot are smallerElements right of pivot are greater
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the sorted position
45
Elements left of pivot are smallerElements right of pivot are greater
So pivot has divided the array into two sub array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the sorted position
45
Elements left of pivot are smallerElements right of pivot are greater
Left sub array Right sub array
So pivot has divided the array into two sub array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the sorted position
45
Elements left of pivot are smallerElements right of pivot are greater
Left sub array Right sub array
So pivot has divided the array into two sub array We will now quick sort
the left sub array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 4Left =1
Is Pivot < Left( 4 < 1)
0 1 2 3 4 5
4 2 3 1 5 6
Pivot
Pivot = 4Left =1
Is Pivot < Left( 4 < 1)
NO
Left Right
0 1 2 3 4 5
4 2 3 1 5 6
Pivot
Pivot = 4Left =1
Is Pivot < Left( 4 < 1)
NOSo we swap pivot and right
Left Right
0 1 2 3 4 5
1 2 3 4 5 6
Pivot
Pivot = 4Left =1
Is Pivot < Left( 4 < 1)
NOSo we swap pivot and right
Left Right
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
Pivot
Pivot = 4Left =1
Is Pivot < Left( 4 < 1)
NOSo we swap pivot and right
Now move the pivot to right
0 1 2 3 4 5
1 2 3 4 5 6
Pivot
Pivot = 4Left =1
Is Pivot < Left( 4 < 1)
NOSo we swap pivot and right
Left Right
0 1 2 3 4 5
1 2 3 4 5 6
The Array is Sorted
Jeanie Lyn Arnoco