You are on page 1of 9

Computer Theory Lab.

7.1 Description of quicksort


 Divide
 Conquer

 Combine

Chapter 7 P.1
Computer Theory Lab.

Chapter 7 P.2
Computer Theory Lab.

Partition(A, p, r)
1 x  A[r]
2 i p–1
3 for j  p to r -1
4 do if A[j] ≤ x
5 then i  i + 1
6 exchange A[i]  A[j]
7 exchange A[i +1]  A[r]
8 return i +1
Chapter 7 P.3
Computer Theory Lab.

At the beginning of each iteration of the loop of


lines 3-6, for any array index k,

1. if p ≤ k ≤ i, then A[k] ≤ x.
2. if i + 1 ≤ k≤ j -1, then A[k] > x.
3. if k = r, then A[k] = x.

Chapter 7 P.4
Computer Theory Lab.

The operation of Partition on a sample array

Chapter 7 P.5
Computer Theory Lab.

Two cases for one iteration of procedure


Partition

Complexity:
Partition on A[p…r] is (n)
where n = r –p +1

Chapter 7 P.6
Computer Theory Lab.

7.2 Performance of quicksort


W o rs t-c a s e p a rtitio n :

T (n )  T (n  1 )   (n )
n n 2
   (k )   (  k )   (n )
k 1 k 1

B e s t-c a s e p a rtitio n :

T (n )  2 T (n / 2 )   (n )
 T ( n )   ( n lo g n )

Chapter 7 P.7
Computer Theory Lab.

Balanced partition T ( n )  ( n log n )


T (n)  T (9n / 10)  T (n / 10)  (n)
 T (n)  (n log n)
Chapter 7 P.8
Computer Theory Lab.

Intuition for the average case T(n) = (n


logn)

Chapter 7 P.9

You might also like