插入排序_C語言_數組 include void insertSort(int ); int main(int argc, const char argv[]) { //初始化數組 int a[10] = {1, 6, 8, 9, 3, 2, 4, 5, 7, 0}; //亂序 printf("亂序 ...
插入排序_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;
}
}