- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertSort.cpp
More file actions
Latest commit
28 lines (26 loc) · 714 Bytes
/
Copy pathinsertSort.cpp
File metadata and controls
28 lines (26 loc) · 714 Bytes
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
#include"main.h"
//直接插入排序的具体实现
voidinsertSort(int testData[])
{
int tempData[testDataCount];
int key, numToCompare;
//转存到临时数组
for (int i = 0; i < testDataCount; i++)
{
tempData[i] = testData[i];
}
for (int i = 1; i < testDataCount; i++)
{
key = tempData[i];
numToCompare = i - 1;
//比key值大的元素依此后移,给key留出空间
while (numToCompare >= 0 && key < tempData[numToCompare])
{
tempData[numToCompare + 1] = tempData[numToCompare];
numToCompare--;
}
tempData[numToCompare + 1] = key;
printCurrentResult(tempData, i);
}
printCurrentResult(tempData, -1);
}