Lec 38 Java Fundamental of Computing
Lec 38 Java Fundamental of Computing
I Semester 2008-09
Lecture 38
1
Number of instructions taken in Quick Sort
2
Quick Sort uses the method Partition
3
What does Partition(int[] A, int left, right) do ?
Left Right
A
... x ...
<x >x
We used x=A[right]
Number of instructions = c(right - left)
4
What does Partition(int[] A, int left, right) do ?
Left Right
A
... x ...
<x >x
We used x=A[right]
Number of instructions = c(right - left)
5
What does Partition(int[] A, int left, right) do ?
Left Right
A
... x ...
<x >x
We used x=A[right]
Number of instructions = c(right - left), for some constant c
6
public static void Qsort(int[] A, int left, int right)
{
if(left<right)
{
int mid = partition(A, left, right);
Qsort(A,left,mid-1);
Qsort(A,mid+1,right);
}
}
mid is the position of pivot element after partition()
7
Number of instructions taken in Quick Sort on n numbers
8
Quick sort on {1, 9, 5, 21, 40, 29, 13}
QSort(A,0,6)
Level 2
QSort(A,4,6)
QSort(A,0,2)
Level 1
Level 0
9
Number of instructions taken in Quick Sort on n numbers
10
Quick sort on {40, 29, 21, 13, 9, 5, 1}
QSort(A,0,6)
Level 6
QSort(A,1,6)
QSort(A,0,−1) Level 5
QSort(A,1,0) QSort(A,2,6)
Level 4
QSort(A,2,1) QSort(A,3,6)
Level 3
QSort(A,3,2) QSort(A,4,6)
Level 2
QSort(A,5,6)
QSort(A,4,3) Level 1
QSort(A,5,4) QSort(A,6,6)
Level 0
11
Number of instructions taken in Quick Sort on n numbers
• If in each recursive call, the pivot element partitions the array into equal half
always, then the number of instructions is =
????
• If in each recursive call, the pivot element is always either the smallest or
greatest, then the number of instructions is =
cn(n − 1).
12
Number of instructions taken in Quick Sort on n numbers
• If in each recursive call, the pivot element partitions the array into equal half
always, then the number of instructions is =
cn log n
• If in each recursive call, the pivot element is always either the smallest or
greatest, then the number of instructions is =
cn(n − 1).
13
Number of instructions taken in Quick Sort on n numbers
• If in each recursive call, the pivot element partitions the array into equal half
always, then the number of instructions is =
cn log n.
• If in each recursive call, the pivot element is always either the smallest or
greatest, then the number of instructions is =
?????.
14
Number of instructions taken in Quick Sort on n numbers
• If in each recursive call, the pivot element partitions the array into equal half
always, then the number of instructions is =
cn log n.
• If in each recursive call, the pivot element is always either the smallest or
greatest, then the number of instructions is =
dn2 , for some constant d.
15
Why is Quick Sort still the most efficient practically ?
2. The fraction of premutations which correspond to the worst case is very very
small.
3. As a consequence, the average number of steps are close to the best case :
O(n log n).
16
Why is Quick Sort still the most efficient practically ?
2. The fraction of premutations which correspond to the worst case is very very
small.
3. As a consequence, the average number of steps are close to the best case :
O(n log n).
17
Which of the two algorithms would you call faster than the other?
18
Which of the two algorithms would you call faster than the other?
19
Which of the two algorithms would you call faster than the other?
20
Which of the two algorithms would you call faster than the other?
21
Interesting question on the following slide ...
22
Which of the two algorithms would you call faster than the other?
23
Which of the two algorithms would you call faster than the other?
24
Realize the following important fact
25
Which of the two algorithms would you call faster than the other?
26
Time complexity of an algorithm
Definition :
27
Worst case time complexity of an algorithm
Definition : Over all inputs of size n, what is the worst case number of steps
taken by the algorithm ?
28