-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path004insertSort.cpp
More file actions
43 lines (39 loc) · 1009 Bytes
/
Copy path004insertSort.cpp
File metadata and controls
43 lines (39 loc) · 1009 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
void insertSort(int a[], int n){
if(n<=1)
return ;
int tmp; // 用来存储待插入的元素
for(int i=1;i<n;i++){
// 从前往后遍历
tmp = a[i];
int j;
for(j=i-1;j>=0;j--){
// 需要将tmp和a[i-1~0]进行比较,直到找到比tmp小的位置为止
if(a[j] > tmp){
// a[j]将元素右移一位
a[j+1] = a[j];
}else{
// 找到合适位置
break;
}
}
// 将tmp填充在合适的位置
a[j+1] = tmp;
// debug
for(int k=0;k<n;k++){
cout << a[k] << " ";
}
cout << endl;
}
cout << endl;
}
// 插入排序法
int main(){
int a[] = {5, 7, 1, 3, 6, 2, 4};
int n = sizeof(a)/sizeof(int);
insertSort(a, n);
for(int i=0;i<n;i++){
cout << a[i] << " ";
}
}