Direct Insert sort//Assume that the records to be sorted are stored in the array R[1..N]. Initially, r[1] self into 1 ordered areas, unordered area is R[2..N]. From i=2 up to i=n, sequentially insert r[i] into the current ordered area R[1..i-1],//Generate an ordered area with N Records. Xin Yang # include <stdio.h> #include <stdlib.h> #define N 10void insertsort (int a[], int size) {int I, j, k, temp = 0; for (i = 1; i < size; i++) {temp = a[i];for (j = 0; J < i; J + +) {if (temp < a[j]) {if (k = i-1; k >= J; k--) {a[k + 1] = a[k];} A[J] = Temp;break;}}} int main () {int m = 0;int B[n] = {9, 8, 7, 6, 5, 4, 3, 2, 1};p rintf ("=============================\n\n");p rintf ("The data before the sort is: \ n 9 8 7 6 5 4 3 2 1\n "); Insertsort (b, N);p rintf (" The result of sorting is: \ n "); for (m = 0; m < n; m++) {printf ("%d ", B[m]);} printf ("\n\n=============================\n\n"); return 0;}
Results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
---c language implementation of data structure direct insertion sort