# Insertion sort void insertion_sort (int a[ ],int N) { int i, j, v; a[0] = INT_MIN; for (i=2; i<=N; i++) { v=a[i]; j=i; while ( a[j-1]>v) { a[j]=a[j-1];

## Presentation on theme: "Insertion sort void insertion_sort (int a[ ],int N) { int i, j, v; a[0] = INT_MIN; for (i=2; i<=N; i++) { v=a[i]; j=i; while ( a[j-1]>v) { a[j]=a[j-1];"— Presentation transcript:

Insertion sort void insertion_sort (int a[ ],int N) { int i, j, v; a[0] = INT_MIN; for (i=2; i<=N; i++) { v=a[i]; j=i; while ( a[j-1]>v) { a[j]=a[j-1]; j--; } a[j]=v; }

void ordina(int a[], const int n) { int i, j, v, t; for (i=n;i>1;i--) if (a[i-1]>a[i]){ t = a[i-1]; a[i-1]= a[i]; a[i]=t; } // minimo nella prima posizione for (i=2; i<=n; i++){ v=a[i]; j=i; while ( a[j-1]>v) { a[j]=a[j-1]; j--; } a[j]=v; } Algoritmo Insertion sort senza sentinella

13 Int Min 3254 i 01 23 4 5 v j Insertion sort NO SI 2 32 1 2 Presentazione realizzata dall’ing. G. Marti v=a[i]; j=i a[j-1]>v a[j]=a[j-1]; j-- a[j]=v i++ 1 il ciclo continua …

Similar presentations