四种排序方法 C 实现

插入法

复制代码
 1#include "stdio.h"
 2#include "stdlib.h"
 3#include "time.h"
 4
 5#define LEN 10
 6
 7int RandNumber();
 8void 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}

32int RandNumber(){
33    return rand()*10000/RAND_MAX;

34}

复制代码


冒泡法

 

复制代码
 1#include "stdio.h"
 2#include "stdlib.h"
 3#include "time.h"
 4
 5#define LEN 10
 6
 7int RandNumber();
 8void 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}

34int RandNumber(){
35    return rand()*10000/RAND_MAX;
36}

37
复制代码

 

 选择法

复制代码
 1#include "stdio.h"
 2#include "stdlib.h"
 3#include "time.h"
 4
 5#define LEN 10
 6
 7int RandNumber();
 8void 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}

35int RandNumber(){
36    return rand()*10000/RAND_MAX;
37}

38
复制代码

 

 希尔排序

 

复制代码
 1#include "stdio.h"
 2#include "stdlib.h"
 3#include "time.h"
 4
 5#define LEN 15
 6
 7int RandNumber();
 8void 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}

39int RandNumber(){
40    return rand()*10000/RAND_MAX;
41}

42
复制代码

 

来自张子阳大哥的教程..此为学习笔记
http://www.cnblogs.com/JimmyZhang/archive/2008/10/02/1303137.html

posted @   Dreampuf  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示