c语言的几种常用排序的实现

几种常见的排序写法:冒泡,选择,快速,插入

/*
 ======================================================
 Name        : some_sort.c
 Author      : fzl
 Copyright   : Your copyright notice
 Description : sort_way
 =====================================================
 */
//1.快速排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10
void initData(int *arr, int n) /* 生成0~100的随机数,用于排序 */
{
    int i = 0;
    srand((int) time(0));
    for (i = 0; i < n; i++)
    {
        arr[i] = rand() % 100;
    }
}
void prt(int *arr, int n) /* 打印 */
{
    int i = 0;
    for (i = 0; i < n; i++)
    {
        printf("%d, ", arr[i]);
    }
    puts("");
}

void quick_sort(int *arr, int n) /* 快速排序, arr数据首地址,n数组数据长度 */
{
    if (n < 2)
        return; /* 至少2个数才进行排序,  同时也是递归中止条件  */
    int low = 0; /* low左指针 */
    int high = n - 1; /* high右指针 , 数组下标从0开始,所以 -1 */
    int key = arr[0]; /* 取基准key,留出第一个左侧的空位 */

    while (low < high) /* low与high碰头就结束 */
    {
        /* 从右->左比较 ,遇到 key>high停止 */
        while (low < high && key < arr[high])
            high--;/* 往左移位置*/
        /* 如果指针未相碰,则把小数 交换 到左侧空位 */
        if (low < high)
            arr[low++] = arr[high]; /* 空位放过数据以后,往右移一个位置low++ */
        /* 从左->右比较, 遇到 key<low停止 */
        while (low < high && key > arr[low])
            low++; /* 往右移位置*/
        /* 如果指针未相碰,则把大数 交换 到右侧空位 */
        if (low < high)
            arr[high--] = arr[low]; /* 空位放过数据以后,往左移一个位置high-- */
    }
    arr[low] = key;/* 关键字放回空间 */
    quick_sort(arr, low);/* low左侧 二分递归 */
    quick_sort(arr + low + 1, n - low - 1); /* high右侧 二分递归 */
}

int main(void)
{
    int a[N];
    initData(a, sizeof(a) / sizeof(int));
    prt(a, sizeof(a) / sizeof(int));

    quick_sort(a, sizeof(a) / sizeof(int));
    prt(a, sizeof(a) / sizeof(int));
    return EXIT_SUCCESS;
}

//2、冒泡排序
#include <stdio.h>
void main()
{
    int a[11],i,j,t;
    printf("input 11 numbers:\n");
    for(i=0;i<11;i++)
        scanf("%d",&a[i]);
    printf("\n");
    for(j=1;j<=10;j++)
        for(i=0;i<=10-j;i++)
            if(a[i]>a[i+1])
            {
                t=a[i];
                a[i]=a[i+1];
                a[i+1]=t;
            }
    printf("the sorted numbers:\n");
    for(i=0;i<11;i++)
        printf("%d ",a[i]);
    printf("\n");
}

//3、选择排序
#include <stdio.h>
void chioce_sort(int *c,int n)
{
    int i,j;
    int temp;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(c[i]>c[j])
            {
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
    }
}
int main()
{
    int i;
    int a[10];
    printf("请输入10个数:\n");
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    printf("\n");
    chioce_sort(a,10);
    printf("选择排序后的序列:\n");
    for(i=0;i<10;i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

//4、插入排序
#include <stdio.h>
#include <stdlib.h>
#define MAX 80
void InsertSort(int R[],int n)
{
    int i,j;
    for(i=2;i<=n;i++)   //依次插入R[2],...,R[n]
        if(R[i]<R[i-1]) 
        {
            R[0]=R[i];  
            j=i-1;
            do        
            {
                R[j+1]=R[j];
                j--;
            }while(R[0]<R[j]);
            R[j+1]=R[0];      //R[i]插入到正确的位置上
        }
}

void main()
{
    int R[MAX];
    int i,n;
    system("clear");
    printf("input number of data:");
    scanf("%d",&n);
    if(n<=0 || n>MAX)
    {
        printf("the number must more than 0 and less than %d.\n",MAX);
        exit(0);
    }
    printf("please input the data:\n");
    for(i=1;i<=n;i++)       //往数组中输入数据
        scanf("%d",&R[i]);
    puts("before sorting:");
    for(i=1;i<=n;i++)
        printf("%d ",R[i]);
    printf("\n");
    InsertSort(R,n);          //调用插入排序进行排序
    puts("\nafter sorting:");
    for(i=1;i<=n;i++)
        printf("%d ",R[i]);
    printf("\n");
}
发布了30 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/fzl_blog/article/details/69789295