c++插入排序程序代码
时间: 2023-02-07 07:55:34 浏览: 121
好的,下面是 C 语言实现的插入排序程序代码:
```
#include <stdio.h>
void insert_sort(int arr[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
int main() {
int arr[] = {5, 3, 8, 6, 1};
int n = sizeof(arr) / sizeof(arr[0]);
insert_sort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
希望这能帮到你!
阅读全文