1 / 8

QUICK SORT

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.

eldora
Download Presentation

QUICK SORT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. QUICK SORT

  2. 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.

  3. 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;

  4. EXAMPLE 1 2 3 4 5 6 7 0 ub ub lb lb ub

  5. 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……

  6. EXAMPLE:- 02 PASS 1: PASS 2: PASS 3:

  7. 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

  8. 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.

More Related