四种排序方法 C 实现
插入法
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 10
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = 1; n < LEN; n++)
18
{
19
j = n;
20
while(j >= 1 && a[j] < a[j - 1]){
21
t = a[j];
22
a[j] = a[j - 1];
23
a[j - 1] = t;
24
j--;
25
}
26
}
27
for(n = 0; n < LEN; n++)
28
printf("%d,", a[n]);
29
30
getchar();
31
}
32
int RandNumber(){
33
return rand()*10000/RAND_MAX;

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}
冒泡法
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 10
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = 0; n < LEN - 1; n++)
18
{
19
for(j = LEN - 1; j >= 1; j--)
20
{
21
if(a[j] < a[j - 1])
22
{
23
t = a[j];
24
a[j] = a[j - 1];
25
a[j - 1] = t;
26
}
27
}
28
}
29
for(n = 0; n < LEN; n++)
30
printf("%d,", a[n]);
31
32
getchar();
33
}
34
int RandNumber(){
35
return rand()*10000/RAND_MAX;
36
}
37

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

选择法
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 10
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t,theMinIndex;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = 0; n < LEN - 1; n++)
18
{
19
theMinIndex = n;
20
for(j = LEN - 1; j > n; j--)
21
if(a[j] < a[theMinIndex])
22
theMinIndex = j;
23
if(theMinIndex != n)
24
{
25
t = a[n];
26
a[n] = a[theMinIndex];
27
a[theMinIndex] = t;
28
}
29
}
30
for(n = 0; n < LEN; n++)
31
printf("%d,", a[n]);
32
33
getchar();
34
}
35
int RandNumber(){
36
return rand()*10000/RAND_MAX;
37
}
38

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

希尔排序
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 15
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t,s,b;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = LEN/2; n >= 1; n = n /2)
18
{
19
for(j = 0; j < n; j++)
20
{
21
for(s = n; s < LEN; s += n)
22
{
23
b = s;
24
while(b >= n && a[b] < a[b - n])
25
{
26
t = a[b];
27
a[b] = a[b - n];
28
a[b - n] = t;
29
b -= n;
30
}
31
}
32
}
33
}
34
for(n = 0; n < LEN; n++)
35
printf("%d,", a[n]);
36
37
getchar();
38
}
39
int RandNumber(){
40
return rand()*10000/RAND_MAX;
41
}
42

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

来自张子阳大哥的教程..此为学习笔记
http://www.cnblogs.com/JimmyZhang/archive/2008/10/02/1303137.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述