c++实现算法插入排序

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using namespace std;
void (int *sortList, int number)
{
int i = 0;
for (; i <= number; i++)
{
cout<<sortList[i]<<" ";
}
cout<<endl;
}
void insertSort( int *sortList, int number)
{
int j = 1;
for (; j <= number; j++)
{
int current = j-1;
int pre = j;
while (current >= 0 && sortList[current] > sortList[pre] )
{
int tmp = sortList[current];
sortList[current] = sortList[pre];
sortList[pre] = tmp;
current--;
pre--;
}
cout<<"the "<<j<<" time: ";
showResult(sortList,number-1);
}
}
int main (int argc, char *argv[])
{
int sortList[10] = {5, 3, 7, 4, 1, 9, 8, 6, 2, 3};
int number = 10;
insertSort(sortList,number-1);
showResult(sortList,number-1);
return 0;
}