酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

#include<iostream> using namespace std;#include<stdlib.h>#include<time.h> const int n=10; const int min=10; const int max=99; int getrand(inmin,int max) { return (rand()%(max-min+1)+min); void set(int r[],int n) { int i; for(i=0; i<n; i++) r[i]=getrand(min,max); void show(intr[],int n){ int i; for(i=0; i<n; i++) cout<<r[i]<<""; cout<<endl; void insertsort(int r[],int n){//由你完成} int main() { srand(time(0)); int a[n]; set(a,n); cout<<"原序列:"; show(a,n); cout<<"新序列: \n; insertsort(a,n);//show(a,n);

以下是一个插入排序的函数实现: ```cpp void insertsort(int r[],int n) { int i, key, j; for (i = 1; i < n; i++) { key = r[i]; j = i - 1; while (j >= 0 && r[j] > key) { r[j + 1] = r[j]; j = j - 1; } r[j + 1] = key; } } ``` 在主函数中调用 `insertsort` 函数对数组进行排序,并输出排序前后的数组元素。 [2024-10-31 10:29:38 | AI写代码神器 | 147点数解答]

相关提问