Quick Sort
-
Upload
priyankanaidu6 -
Category
Technology
-
view
3.333 -
download
5
description
Transcript of Quick Sort
QUICK SORT
Quick sort• Uses divide-and-conquer approach
• Divide: If S has at least two elements select a specific element x from S, called the pivot. Partition S into
– L storing elements in S less than x
– E storing elements in S equal to x
– G storing elements in S greater than or equal to x
• Recur: recursively sort sequences L and G
• Conquer: put elements in L, E and G back into S in order
GIVEN SERIES
5 3 8 9 1 7 0 2 6 4
SELECT THE PIVOT ELEMENT
P
5 3 8 9 1 7 0 2 6 4
P L R
ITERATION-1
5 3 8 9 1 7 0 2 6 4
SWAPE THE ELEMENTS
5 3 4 9 1 7 0 2 6 8
5 3 4 9 1 7 0 2 6 8
P L R
5 3 4 2 1 7 0 9 6 8
SWAP THE ELEMENTS
5 3 4 2 1 7 0 9 6 8
P L R
SWAP THE ELEMENTS
5 3 4 2 1 0 7 9 6 8
P R L
0 3 4 2 1 5 7 9 6 8
P > R > L
0 3 4 2 1 5 7 9 6 8
0 1 4 2 3 5 7 6 9 8
P L R P L R
0 3 4 2 1 5 7 9 6 8
ITERATION-2
SWAP THE ELEMENTS
0 1 4 2 3 5 6 7 9 8
P L R P L R
SWAP THE ELEMENTS
0 1 2 4 3 5 6 7 9 8
5 6 7 9 8 0 1 2 4 3
5 6 7 8 90 1 2 3 4
SWAP THE ELEMENTS
0 1 2 3 4 5 6 7 8 9