Data Structure --- direct insertion and sorting in c language --- direct insertion in c
// Insert the sorting directly. // assume that the record to be sorted is stored in the array R [1. n. // At the beginning, R [1] is a self-contained ordered zone, and the disordered zone is R [2. n]. // From I = 2 until I = n, insert R [I] into the current sequential zone R [1 .. in I-1], // generates an ordered zone containing n records. // Yang Xin # include <stdio. h> # include <stdlib. h> # define N 10 void 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]) {for (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 }; printf ("=================================\ n \ n "); printf ("the data before sorting is: \ sort 8 7 6 5 4 3 2 1 \ n"); InsertSort (B, N); printf ("the result after sorting is: \ n "); for (m = 0; m <N; m ++) {printf (" % d ", B [m]);} printf ("\ n =============================\ n \ n "); return 0 ;}
Result:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.