Escolar Documentos
Profissional Documentos
Cultura Documentos
QUICK SORT
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
Quick sort
7
After partitioning, repeat the sort on the left and right sides
Quick sort
8
pivot
Quick sort
9
1.
QUICKSORT(A[p,r])
Input: A[p,.r]
Output: the sub array A[1r] stored in non decreasing order
If p<r
q= PARTITION(A,p,r)
QUICKSORT(A[p,q-1])
QUICKSORT(A[q+1, r])
v
S
v
S1
S2
PARTITION(A[pr])
Initialization
P=A[p]
i=p
j=r+1
Repeat
Swap(A[i],A[j])
Until i j
Swap(A[p],a[j]);
Return j
Space complexity
Time complexity
Choosing pivot element
Space complexity:
Space for quick sort:
Time complexity
Best case : n log2n
Worst case : n2
Average case : 1.38 n log2n
an algorithm.
Median of three partition method uses pivot as the
other sorts.
Idea
applications.
THANK YOU