-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSORT_insert.cpp
More file actions
39 lines (39 loc) · 928 Bytes
/
Copy pathSORT_insert.cpp
File metadata and controls
39 lines (39 loc) · 928 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
/*
=====插入排序================================================
***在要排序的一组数中,假设前面(n-1)个数已经是排好顺序的,
现在要把第n个数插到前面有序的数中,使得这n个数也是排好顺序的
如此循环直至排序完成***
*初始状态 【57 68 59 52】
step1: 【57 | 68 59 52】68>57,不处理
step2: 【57 68 | 59 52】59<68,应为57<59<68,故59插在57之后
step3:【57 59 68 | 52】52<57,插在57之前
【52 57 59 68】
=====完成=====================================================
*/
#include<stdio.h>
int main()
{
int a[] = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int n = 28;
int i, j, temp;
for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
for(i = 1; i < n; i++)
{
temp = a[i];
for(j = i-1; j >= 0 && temp < a[j]; j--)
{
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
return 0;
}