90 likes | 761 Views
QUICK SORT. INTRODUCTION. Most widely used Internal sorting Algorithm. Also known as PARTITION EXCHANGE SORT . Its basic form was invented by “ C.A.R. Hoare ”. Based on DIVIDE & CONQUER technique. TERMS USED:-- low; high; pivot; lb, ub. EXAMPLE. 1. 2. 3. 0. 4. 5. 6. 7. ub.
E N D
INTRODUCTION • Most widely used Internal sorting Algorithm. • Also known as PARTITION EXCHANGE SORT. • Its basic form was invented by “ C.A.R. Hoare”. • Based on DIVIDE & CONQUER technique.
TERMS USED:-- low; high; pivot; lb, ub EXAMPLE 1 2 3 0 4 5 6 7 ub lb ub ub lb ub 2 3 0 4 5 6 7 1 STOP STOP Till x[lb] <= pivot; then increase lb by 1; ub lb SWAP x[lb] & x[ub] Till x[ub] >= pivot; decrease ub by 1;
EXAMPLE 1 2 3 4 5 6 7 0 ub ub lb lb ub
EXAMPLE 1 2 3 4 5 6 7 0 ub lb ub CONDITION FAILED………… So, SWAP x[low] & x[ub] CONTINUE TILL ENTIRE ARRAY IS SORTED……
EXAMPLE:- 02 PASS 1: PASS 2: PASS 3:
IMPLEMENTATION Algorithm: Quick Sort (low, high, x, n) // n is total number of elements, low=0, high=n-1 Step 1: if (low<high) then k=partition(low, high, x, n); quicksort(low, k-1, x, n); quicksort(k+1, high, x, n); end if end quicksort
ALGORITHM:- Partition(low, high, x, n) Step 1:- pivot = x[low]; lb=low; ub=high; Step 2:- while(lb<ub) { while(x[lb]<=pivot && lb<high) lb=lb+1; while(x[ub]>pivot) ub=ub-1; if(lb<ub) { temp=x[lb]; x[lb]=x[ub]; x[ub]=temp; } } x[low]=x[ub]; x[ub]=pivot; return ub; End partition.