Correctness and Complexity Analysis of Quick Sort
Correctness and Complexity Analysis of Quick Sort
ASSIGNMENT
(a) Full Name : Divya Arya
Partiton ( A , p ,r)
5. x A[ r ]
6. i p – 1
7. for j p to r-1
8. if A [ j ] <= x
9. then i i+1
10. swap( A [ i ] , A [ j ] )
11. swap(A [ i +1 ] , A [ r ]
12. return i + 1
LOOP INVARIANT
MAINTAINANCE : When ‘x’ and ‘i’ are initialized iteration begins then
the two conditions are possible . Suppose A [ j ] > x , the value of ‘i’
will not be changed and ‘j’ becomes ‘j + 1‘ . Suppose after all iterations
that is till r-1 , A[ j ] >x so ‘i’ will not be changed and A [ j ] is also
unchanged . Suppose A[ j ] < = x then x and A [ i + 1 ] are swapped
and ‘i’ is incremented by 1 and the loop invariant holds .
.
Worst Cases : The worst case of quicksort O(n2) can be
avoided by using randomized quicksort. It can be easily
avoided with high probability by choosing the right pivot.
Obtaining an average case behavior by choosing right
pivot element makes it improvise the performance and
becoming as efficient as Merge sort