1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| #include <iostream>
using namespace std;
void print(int a[],int n){
for (int i=0; i<n; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
void InsertSort(int a[],int n){
for (int i=1; i<n; i++) {
if (a[i]<a[i-1]) {
int j = i-1;
int x = a[i];
a[i] = a[i-1];
while (x<a[j]) {
a[j+1] = a[j];
j--;
}
a[j+1] = x;
print(a, n);
}
}
}
int main(int argc, const char * argv[]) {
int a[] = {9,7,5,8,7,9,9};
InsertSort(a, 7);
return 0;
}
|