Quick Sort: - . - DR V K Pathak
Quick Sort: - . - DR V K Pathak
on
QUICK SORT
Submitted to :
Dr. V. K. Pathak
Submitted by:
Abhishek Kumar Shukla
88/07
III rd B.Tech
070451
Topics to be
covered
• Quick sort as Devide & conquer
method
• Algorithm of quick sort
• Analysis of complexity of quick sort
• Programs of quick sort
• Outputs of sorting algorithm
Quick Sort :
Introduction
• One of the fastest in-memory sorting algorithms
• Proposed by Tony Hoarein 1962.
• The algorithm is a demonstration of the divide-
and conquer strategy.
• It first divides the array into two subarrays (of
arbitrary sizes) in such a way that every element
of the first part is smaller than or equal to every
element of the second part - partition.
• The two subarrayscan be sorted individually,
within their own areas. The final array is sorted -
without any merging process.
• The average-time complexity of Quicksort is O(n
D iv id e - a n d -
C o n
Divide and Conquer
design.
q
is au e
methodrof algorithm
This method has three distinct steps:
–Divide: If the input size is too large to
deal with in a straightforward manner,
divide the data into two or more disjoint
subsets.
–Recur: Use divide and conquer to solve the
subproblems associated with the data
subsets.
–Conquer: Take the solutions to the
subproblems and “merge” these solutions
into a solution for the original problem.
Quick - Sort
1) Divide : If the sequence S has 2 or
more elements, select an element x from S
to be your pivot. Any arbitrary element,
like the first, will do. Remove all the
elements of S and divide them into 3
sequences:
L, holds S’s elements less than x
E, holds S’s elements equal to x
G, holds S’s elements greater than x
2) Recurse: Recursively sort L and G
3) Conquer: Finally, to put elements back
into S in order, first inserts the
elements of L, then those of E, and those
of G.
Quicksort Algorithm
Given an array of n elements (e.g.,
integers):
If array only contains one element, return
Else
–pick one element to use as pivot.
–Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
–Quicksort two sub-arrays
–Return results
Example
We are given array of n integers to
sort:
40 20 10 80 60 50 7 30 100
Pick Pivot Element
There are a number of ways to pick
the pivot element. In this example,
we will use the first element in
the array:
10
40 20 10 80 60 50 7 30 0
Partitioning Array
Given a pivot, partition the elements of
the array such that the resulting array
consists of:
1.One sub-array that contains elements
>= pivot
2.Another sub-array that contains elements
< pivot
3.
The sub-arrays are stored in the original
data array.
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
pivot_index = 0 10
40 20 10 80 60 50 7 30 0
[0] [1] [2] [3] [4]
[5] [6] [7] [8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
pivot_index = 0 10
40 20 10 80 60 50 7 30 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
pivot_index = 0 10
40 20 10 80 60 50 7 30 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1.
pivot_index = 0 10
40 20 10 80 60 50 7 30 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1.
pivot_index = 0 10
40 20 10 80 60 50 7 30 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1.
pivot_index = 0 10
40 20 10 80 60 50 7 30 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 60 50 7 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 10
40 20 10 30 7 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
10
pivot_index = 4 7 20 10 30 40 50 60 80 0
[0] [1] [ 2] [ 3] [4] [5] [6] [ 7] [ 8]
too_big_index too_small_index
Partition Result
10
7 20 10 30 40 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
10
7 20 10 30 40 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
1 .While data[too_big_index] <= data[pivot]
++too_big_index
1 .While data[too_small_index] > data[pivot]
--too_small_index
1 .If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
1 .While too_small_index > too_big_index, go to 1.
2 .Swap data[too_small_index] and data[pivot_index]
1.
pivot_index = 0 2 4 10 12 13 50 57 63 10
0
[0] [1] [2] [3] [4] [5] [6] [7]
too_big_index too_small_index
Worst case
l Worst case?
l Each call to Partition splits the array into an empty
array and n-1 array
l
l
l
l
l
l
l
l
l
l
Quicksort Analysis
• Assume that keys are random, uniformly
distributed.
• Best case running time: O(n log2n)
• Worst case running time?
– Recursion:
1.Partition splits array in two sub-arrays:
• one sub-array of size 0
• the other sub-array of size n-1
2.Quicksort each sub-array
– Depth of recursion tree? O(n)
– Number of accesses per partition? O(n)
–
–
Quicksort Analysis
• Assume that keys are random, uniformly
distributed.
• Best case running time: O(n log2n)
• Worst case running time: O(n2)!!!
–
W o rst C a se
= Θ( n 2 )
Worst Case
Worse case running time
T (n) = T (n − 1) + Θ(n)
vo id m a in ()
{
in t n , ia
, [M A X ];
clo ck_t sta rt, e n d ;
clrscr();
ra n d o m ize ();
g e tch ();
}
vo id q u ick_so rt( in t a [ ] , in t
li
, nt u)
{
in t j;
if( l< u )
{
j= p a rtitio n ( a , lu
, );
q u ick_so rt( a , lj , -1 );
q u ick_so rt( a , j+ 1 , u );
}
}
in t p a rtitio n ( in t a [ ] , in t li
, nt
u)
{
in t v , i, j, te m p ;
v = a [ l] ; i= l; j= u + 1 ;
do
{
do
i+ + ;
w h ile ( a [ i] < v &&i< = u );
do
j--;
w h ile ( a [ j] > v );
if( i< j)
{
te m p = a [ i] ;
a [ i] = a [ j] ;
a [ j] = te m p ;
}
} while ( i< j);
a [ l] = a [ j] ;
a [ j] = v ;
re tu rn ( j);
}
Outputs
E n te r n o . o f e le m e n ts : 1 0
U n so rte d d a ta ->
852 7203 8009 3791 9857 9386
3427 3275 3368 4027
S o rte d d a ta ->
852 3275 3368 3427 3791 4027
7203 8009 9386 9857
T h e tim e e la p se d ( in se c ): 0 . 0 0 0 0 0 0
Enter no. of elements :50