插入排序_C语言_数组
#include <stdio.h>
void insertSort(int *);
int main(int argc, const char * argv[]) {
//初始化数组
int a[10] = {1, 6, 8, 9, 3, 2, 4, 5, 7, 0};
//乱序
printf("乱序\n");
for (int i = 0; i < 10; i ++ ) {
printf("%d ",a[i]);
}
printf("\n\n");
insertSort(a);
//顺序
printf("顺序\n");
for (int i = 0; i < 10; i ++ ) {
printf("%d ",a[i]);
}
printf("\n\n");
return 0;
}
void insertSort(int * a) {
int i = 1;
int temp;
int pos;
for (i; i < 10; i ++) {
//所要插入并排序的数值
temp = a[i];
pos = i - 1;
while (temp < a[pos]) {
a[pos + 1] = a[pos];
pos --;
}
a[pos + 1] = temp;
}
}