QUICK SORT
DIVIDE AND CONQUER
1
DIVIDE AND CONQUER
 An algorithm is only called divide and conquer if it
contains two or more recursive calls
 The divide and conquer strategy used to
 make a more efficient search algorithm
 can also be applied to sorting
2
DIVIDE AND CONQUER (CONT.)
 Divide : Divide the problem P into P1 , P2,……Pk sub
problems until P is smaller one
 Conquer : the sub-problems by solving them recursively
 Combine : the solutions of sub problems to create a
solution to the original problem
3
4
A problem size n
Sub problem 1 of
size n/2
Sub problem 2 of
size n/2
A solution to
sub problem 1
A solution to
sub problem 2
A solution to the original
problem
DIVIDE AND CONQUER (CONT.)
DIVIDE AND CONQUER - EXAMPLE
 Sorting: merge sort and quicksort
 Binary tree traversals
 Binary search
 Multiplication of large integers
 Matrix multiplication: Strassen’s algorithm
 Closest-pair and convex-hull algorithms
5
MERITS OF DIVIDE AND CONQUER
6
 Time spent on executing the problem using divide and
conquer is smaller than others.
 Provides efficient algorithm.
 Suited for parallel computation in which each sub problem
can be solved simultaneously by its own processor.
Quick sort
 The main idea is to partition the array into two regions:
small items are moved to the left side of the array
large items are moved to the right side
 After partitioning, repeat the sort on the left and right sides
each region is a sub-problem, a smaller version of the
original problem
7
Quick sort
8
 Main question: how do we decide which items are “small”
and which are “large”?
 A common technique: use the first item in the region as a
pivot
everything less than the pivot ends up in the left region
items greater than or equal to the pivot go in the right
region
Quick sort
1. Divide: partition A[p..r] into two sub arrays A[p..q-1] and
A[q+1..r] such that each element of A[p..q-1] is ≤ A[q],
and each element of A[q+1..r] is ≥ A[q]. Compute q as part
of this partitioning.
2. Conquer: sort the sub arrays A[p..q-1] and A[q+1..r] by
recursive calls to QUICKSORT.
3. Combine: the partitioning and recursive sorting leave us
with a sorted A[p..r]
9
Quick sort algorithm
QUICKSORT(A[p,…r])
 Input: A[p,….r]
 Output: the sub array A[1…r] stored in non decreasing order
If p<r
 q= PARTITION(A,p,r)
 QUICKSORT(A[p,q-1])
 QUICKSORT(A[q+1, r])
10
v
v
S1 S2
S
Quick sort algorithm (Cont.)
11
PARTITION(A[p…r])
Initialization
 P=A[p]
 i=p
 j=r+1
Quick sort algorithm (Cont.)
12
 Repeat
Repeat i=i+1 until A[i]≥P
Repeat j=j-1 until A[j]≤P
Swap(A[i],A[j])
 Until i ≥ j
 Swap(A[p],a[j]);
 Return j
Quick sort algorithm (Cont.)
13
Void swap( int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
COMPLEXITY OF QUICK SORT
14
 Space complexity
 Time complexity
 Choosing pivot element
15
Space complexity:
 Space for quick sort:
Each and every recursive call require stack space
for an array that is q.
 Space for partition:
Space for an array A=‘n’ locations
Space for control variable= 3 location (i,j,pivot)
COMPLEXITY OF QUICK SORT (Cont.)
16
Time complexity
 Best case : n log2n
 Worst case : n2
 Average case : 1.38 n log2n
COMPLEXITY OF QUICK SORT (Cont.)
17
Choosing pivot element
 Selection of proper pivot, improve the efficiency of
an algorithm.
 Median of three partition method uses pivot as the
median of left most, right most, and middle element
of the array.
COMPLEXITY OF QUICK SORT (Cont.)
BENEFITS OF QUICK SORT
18
 Sorting time is very less when comparing with all
other sorts.
 Idea of partitioning can be useful in many
applications.
Divide and conquer - Quick sort

Divide and conquer - Quick sort

  • 1.
  • 2.
    DIVIDE AND CONQUER An algorithm is only called divide and conquer if it contains two or more recursive calls  The divide and conquer strategy used to  make a more efficient search algorithm  can also be applied to sorting 2
  • 3.
    DIVIDE AND CONQUER(CONT.)  Divide : Divide the problem P into P1 , P2,……Pk sub problems until P is smaller one  Conquer : the sub-problems by solving them recursively  Combine : the solutions of sub problems to create a solution to the original problem 3
  • 4.
    4 A problem sizen Sub problem 1 of size n/2 Sub problem 2 of size n/2 A solution to sub problem 1 A solution to sub problem 2 A solution to the original problem DIVIDE AND CONQUER (CONT.)
  • 5.
    DIVIDE AND CONQUER- EXAMPLE  Sorting: merge sort and quicksort  Binary tree traversals  Binary search  Multiplication of large integers  Matrix multiplication: Strassen’s algorithm  Closest-pair and convex-hull algorithms 5
  • 6.
    MERITS OF DIVIDEAND CONQUER 6  Time spent on executing the problem using divide and conquer is smaller than others.  Provides efficient algorithm.  Suited for parallel computation in which each sub problem can be solved simultaneously by its own processor.
  • 7.
    Quick sort  Themain idea is to partition the array into two regions: small items are moved to the left side of the array large items are moved to the right side  After partitioning, repeat the sort on the left and right sides each region is a sub-problem, a smaller version of the original problem 7
  • 8.
    Quick sort 8  Mainquestion: how do we decide which items are “small” and which are “large”?  A common technique: use the first item in the region as a pivot everything less than the pivot ends up in the left region items greater than or equal to the pivot go in the right region
  • 9.
    Quick sort 1. Divide:partition A[p..r] into two sub arrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is ≤ A[q], and each element of A[q+1..r] is ≥ A[q]. Compute q as part of this partitioning. 2. Conquer: sort the sub arrays A[p..q-1] and A[q+1..r] by recursive calls to QUICKSORT. 3. Combine: the partitioning and recursive sorting leave us with a sorted A[p..r] 9
  • 10.
    Quick sort algorithm QUICKSORT(A[p,…r]) Input: A[p,….r]  Output: the sub array A[1…r] stored in non decreasing order If p<r  q= PARTITION(A,p,r)  QUICKSORT(A[p,q-1])  QUICKSORT(A[q+1, r]) 10 v v S1 S2 S
  • 11.
    Quick sort algorithm(Cont.) 11 PARTITION(A[p…r]) Initialization  P=A[p]  i=p  j=r+1
  • 12.
    Quick sort algorithm(Cont.) 12  Repeat Repeat i=i+1 until A[i]≥P Repeat j=j-1 until A[j]≤P Swap(A[i],A[j])  Until i ≥ j  Swap(A[p],a[j]);  Return j
  • 13.
    Quick sort algorithm(Cont.) 13 Void swap( int *a, int *b) { int t; t=*a; *a=*b; *b=t; }
  • 14.
    COMPLEXITY OF QUICKSORT 14  Space complexity  Time complexity  Choosing pivot element
  • 15.
    15 Space complexity:  Spacefor quick sort: Each and every recursive call require stack space for an array that is q.  Space for partition: Space for an array A=‘n’ locations Space for control variable= 3 location (i,j,pivot) COMPLEXITY OF QUICK SORT (Cont.)
  • 16.
    16 Time complexity  Bestcase : n log2n  Worst case : n2  Average case : 1.38 n log2n COMPLEXITY OF QUICK SORT (Cont.)
  • 17.
    17 Choosing pivot element Selection of proper pivot, improve the efficiency of an algorithm.  Median of three partition method uses pivot as the median of left most, right most, and middle element of the array. COMPLEXITY OF QUICK SORT (Cont.)
  • 18.
    BENEFITS OF QUICKSORT 18  Sorting time is very less when comparing with all other sorts.  Idea of partitioning can be useful in many applications.