1 #include <stdio.h> 2 #include <string.h> 3 4 //插入排序 5 void insert_sort(int arr[], int lsize); 6 //交换函数 7 void Swap_ele(int *prev, int *late); 8 //遍历数组 9 void PrintfArr(int *arr); 10 11 int main() 12 { 13 int arr[6] = { 3, 6, 1, 2, 9, 7 }; 14 int n = sizeof arr / sizeof *arr; 15 printf("%d ", n); 16 PrintfArr(arr, n); 17 printf(" "); 18 insert_sort(arr, sizeof arr / sizeof *arr); 19 PrintfArr(arr, sizeof arr / sizeof *arr); 20 printf(" "); 21 return 0; 22 } 23 24 //交换函数 25 void Swap_ele(int *prev, int *late) 26 { 27 int n; 28 n = *prev; 29 *prev = *late; 30 *late = n; 31 } 32 33 //插入排序 34 void insert_sort(int arr[], int lsize) 35 { 36 int i, j; 37 38 for (i = 0; i < lsize; i++) 39 { 40 41 for (j = i ; j >= 0 && j < lsize - 1; j--) 42 { 43 int n = arr[j ]; 44 if (arr[j ] < arr[j-1]) 45 { 46 /*arr[j ] = arr[j-1]; 47 arr[j] = n;*/ 48 Swap_ele(&arr[j], &arr[j-1]); 49 } 50 51 } 52 } 53 } 54 55 //遍历数组 56 void PrintfArr(int arr[],int n) 57 { 58 for (int i = 0; i < n ;i++) 59 { 60 printf("%d ", arr[i]); 61 } 62 }