1 / 5

Quick sort

Quick sort. void quick_sort ( int a[], int l, int r) { int i, j,v, t; if (r>l) { v=a[r]; //L’elemento “pivot” è quello più a dx i=l-1; j=r; // i scorre da sx a dx; j da dx a sx for (;;) { while ( a[++i]<v); while ( a[--j]>v)if(j==l) break; if (i>=j) break;

garan
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 void quick_sort (int a[],int l, int r) { int i, j,v, t; if (r>l) { v=a[r];//L’elemento “pivot” è quello più a dx i=l-1; j=r;// i scorre da sx a dx; j da dx a sx for (;;) { while ( a[++i]<v); while ( a[--j]>v)if(j==l) break; if (i>=j) break; t=a[i]; a[i]=a[j]; a[j]=t;//Scambia gli//elementi i e j } t=a[i]; a[i]=a[r]; a[r]=t;//Sistema l’elemento “pivot” quick_sort (a, l, i-1); quick_sort (a, i+1, r); } }

  2. Quick sort i = > j ? NO! Scambio a[i] e a[j] i = > j Scambio a[i] e a[r] r > l l r 3 5 2 7 4 4 1 1 1 5 7 pivot i j a[j] > v? NO a[i] < v 1 4 NO a[j] > v 4 temp v

  3. chiamata ricorsiva quick_sort (a, l, i-1); l i-1 3 1 2 quick_sort (a, i+1, r); i+1 r 7 5

  4. i = > j ? NO! Scambio a[i] e a[j] r > l l r 3 1 2 1 3 2 3 while ( a[++i]<v); j i pivot while ( a[--j]>v); i = > j ? SI! Scambio a[i] e a[r] 1 2 2 temp v

  5. chiamata ricorsiva quick_sort (a, l, i-1); Valore uguale FINE quick_sort (a, i+1, r); Valore uguale FINE

More Related