Eine sehr einfache Implementierung eines Insertion Sorts in C
?Download insertionsort.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdio.h> #define MAX 8 void insertion_sort(int *liste); void output(int *liste); int main(int argc, char* argv[]) { int a[] = {15, 9, 12, 1, 6, 13, 3, 4}; output(a); insertion_sort(a); return 0; } void output(int *liste) { int i=0; for(i=0; i<MAX; i++) { printf("[%2d] ",liste[i]); } printf("n"); } void insertion_sort(int *liste) { int i=0; int j=0; int x=0; for(i=1; i<MAX; i++) { x = liste[i]; j = i-1; while (j >= 0 && x < liste[j]) { printf(" Vergleich: [%2d] < [%2d] n",x,liste[j]); liste[j+1] = liste[j]; //printf(" +> "); //output(liste); j--; } liste[j+1] = x; printf(" Aufbau: "); output(liste); } } |