插入排序(C语言)
2018-12-24 本文已影响0人
巴巴呀呀
#include <stdio.h>
void insertion_sort(int arr[], int len);
void print_arr(int arr[], int len) ;
int main(int argc, const char * argv[]) {
int arr[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
print_arr(arr, 10);
insertion_sort(arr, 10);
print_arr(arr, 10);
return 0;
}
void insertion_sort(int arr[], int len) {
int j;
for (int i = 1; i < len; i++) {
int temp = arr[i];
for (j = i-1; j >= 0 && arr[j] > temp; j--) {
arr[j+1] = arr[j];
}
arr[j+1] = temp;
}
}
void print_arr(int arr[], int len) {
for (int i = 0; i < len; i++) {
printf("%d ",arr[i]);
}
printf("\n");
}