-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertSort
More file actions
44 lines (34 loc) · 759 Bytes
/
Copy pathinsertSort
File metadata and controls
44 lines (34 loc) · 759 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
#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
using namespace std;
void insertSort(int a[],int length) {
for (int i = 1; i < length; i++)
{
int temp;
if(a[i]<a[i-1])//插入a[i]
{
temp=a[i];
int j=i-1;
for(;a[j]>temp&&j>=0;j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
// 待插入元素
}
}
int main()
{
int a[]={2,6,9,8,5,3,4,7};
int length=10;
insertSort(a,length);
for(int i=0;i<9;i++) cout << a[i] <<" ";
cout <<endl;
}