思想:
就是从前往后将每个数字插入到适合他们的位置
15 89 363 78 14 65 47
15 89 363 78 14 65 47
15 89 363 78 14 65 47
15 78 89 363 14 65 47
14 15 78 89 363 65 47
14 15 65 78 89 363 47
14 15 47 65 78 89 363
#include<bits/stdc++.h>
using namespace std;
void insertSort(int a[], int n){
for(int i = 1; i < n; i++){
for(int j = i; j > 0; j--){
if(a[j] < a[j-1]){
int x = a[j];
a[j] = a[j-1];
a[j-1] = x;
}
else{
break;
}
}
}
}
int main(){
int a[10] = {1, 10, 23, 59, 33, 65, 21, 15, 98, 56} ;
insertSort(a, 10);
for(int i = 0; i < 10; i++){
cout << a[i] << endl;
}
return 0;
}
592



被折叠的 条评论
为什么被折叠?



