Auto commit

This commit is contained in:
smallkun 2025-04-03 10:46:38 +08:00
parent 39672f95cb
commit 1a1a6c0409

View File

@ -75,4 +75,40 @@ int main(){
### 插入排序
### 插入排序
```c
#include <stdio.h>
void insertSort(int *a, int n, int flag){
int i, j, t;
for(i=1;i<n;i++){//假设前i位已经有序
t = a[i];//备份当前需要排序的数
j=i-1;//计算出当前位置前一项
while((flag>0?a[j] > t:a[j] < t) && j>=0) {
a[j+1] = a[j];
j--;
}
a[j+1] = t;
}
}
void printArray(int *a, int n){
int i;
for(i=0;i<n;i++){
printf("%d ", a[i]);
}
printf("\n");
}
int main(){
int a[] = {100, 1, 3, 2, 9, -1, 20, 30, 10, 50};
int n = sizeof(a)/4;
insertSort(a, n, 0);
printArray(a, n);
return 0;
}
```