• 数据结构-——排序


    #include <stdlib.h>
    #include <stdio.h>
    #define MAX 50
    int slist[MAX]; /*待排序序列*/
    
    void XierSort(int list[], int n);
    void insertSort(int list[], int n);
    void createList(int list[], int *n);
    void printList(int list[], int n);
    void heapAdjust(int list[], int u, int v);
    void heapSort(int list[], int n);
    
    int Partition(int list[],int low,int high)
    {
        //int low1=low,high1=high;
        int key=list[low];
        while(low<high)
        {
    
            while(low<high && list[high]>=key)
                --high;
            list[low]=list[high];
            while(low<high && list[low]<=key)
                ++low;
            list[high]=list[low];
        }
        list[low]=key;
        return low;
    }
    
    void Quicksort(int list[],int low,int high,int n,int *count)
    {
    
        int h,i;
        if(low<high)
        {
            h=Partition(list,low,high);
            printf("第%d次排序结果:", (*count)++);
            printList(list, n);
            Quicksort(list,h+1,high,n,count);
            Quicksort(list,low,h-1,n,count);
        }
    }
    
    void XierSort(int list[], int n)
    {
        int i,j, node=0, count=1;
        int m;
        printf("对序列进行希尔排序:
    ");
        printf("初始序列为:");
        printList(list, n);
        for(m=n/2; m>0; m/=2)
        {
            for(i=m+1; i<=n; i++)
            {
                node = list[i];
                j=i-m;
                while(j>=1&&node<list[j])
                {
                    list[j+m] = list[j];
                    j=j-m;
                }
                list[j+m] = node;
                printf("第%d次排序结果:", count++);
                printList(list, n);
            }
        }
    }
    
    /*直接插入排序*/
    void insertSort(int list[], int n)
    {
        int i = 1, j = 0, node = 0, count = 1;
        printf("对序列进行直接插入排序:
    ");
        printf("初始序列为:");
        printList(list, n);
        for(i = 2; i <= n; i++)
        {
            node = list[i];
            j = i - 1;
            while(j >= 0 && node < list[j])
            {
                list[j+1] = list[j];
                --j;
            }
            list[j+1] = node;
            printf("第%d次排序结果:", count++);
            printList(list, n);
        }
    }
    
    /*堆排序*/
    void heapAdjust(int list[], int u, int v)
    {
    
        int i = u, j, temp = list[i];
        j = 2 * i;
        while (j <= v)
        {
            if(j < v && list[j] < list[j+1])
                j++; /*若右孩子较大,则把j修改为右孩子的下标*/
            if(temp < list[j])
            {
                list[i] = list[j]; /*将list[j]调到父亲的位置上*/
                i = j;
                j = 2 * i; /*修改i和j的值,以便继续向下筛选*/
            }
            else
                break; /*筛选完成,终止循环*/
        }
        list[i] = temp; /*被筛结点的值放入最终位置*/
    }
    void heapSort(int list[], int n)
    {
        int i = 0, temp = 0, count = 1;
        printf("对序列进行堆排序:
    ");
        printf("初始序列为:");
        printList(list, n);
        for (i = n  / 2; i > 0; i--)
            heapAdjust(list, i, n); /*建立初始堆*/
        printf("建立的初始堆为:");
        printList(list, n);
        for(i = n ; i > 1; i--)
        {
            /*循环,完成堆排序*/
            temp = list[1];
            list[1] = list[i];
            list[i] = temp; /*将第一个元素同当前区间内最后一个元素对换*/
            heapAdjust(list, 1, i-1);  /*筛选出list[1]结点*/
            printf("第%d次排序结果:", count++);
            printList(list, n);
        }
    }
    
    /*生成待排序序列*/
    void createList(int list[], int *n)
    {
        int i = 1,a;
        printf("请输入待排序序列(长度小于50,以输入一个字符结束):
    ");
        while(scanf("%d",&a)==1)
        {
            list[i] = a;
            i++;
        }
        *n=i-1;
        getchar();
    }
    
    /*输出排序结果*/
    
    void printList(int list[], int n)
    {
        int i = 1;
        for(; i <= n; i++)
        {
            printf("  %d  ", list[i]);
            if(i % 10 ==0 && i != 0)
                printf("
    ");
    
        }
        printf("
    ");
    
    }
    
    int main()
    {
    
        int choice=1,length;
        int n=1;
        while(choice!=0)
        {
            printf("
    ");
            printf("***** 内部排序算法演示程序 *****
    ");
            printf("
    1. 直接插入排序 
    ");
            printf("
    2. 堆排序 
    ");
            printf("
    3. 希尔排序 
    ");
            printf("
    4. 快速排序 
    ");
            printf("
    0. 退出
    ");
            printf("
    请选择:");
            scanf("%d", &choice);
            getchar();
            switch(choice)
            {
            case 1:
            {
                createList(slist, &length);
                insertSort(slist, length);
                break;
            }
            case 2:
            {
                createList(slist, &length);
                heapSort(slist, length);
                break;
            }
            case 3:
            {
                createList(slist, &length);
                XierSort(slist, length);
                break;
            }
            case 4:
            {
                createList(slist, &length);
                printf("对序列进行快速排序:
    ");
                printf("初始序列为:");
                printList(slist,length);
                Quicksort(slist,1,length,length,&n);
                break;
            }
            default:
                choice=0;
    
            }
            printf("
    ");
        }
        return 0;
    }
    
    /*输入待排序序列:49 38 65 97 13 27 49(以输入一个字符作为结束)*/
  • 相关阅读:
    【分享】使用 AXI performance monitors (APM)测试MPSoC DDR访问带宽
    测试MPSoC GEM 的1588功能
    使用ffmpeg把mp4转换为NV12文件
    强制开放MPSoC的PS-PL接口
    Java操作数据库指南(之一)
    大数据中台技术架构
    数据仓库到数据中台的演进
    Spark SQL入门到实战之(8)数据倾斜优化
    项目实战从0到1之hive(47)大数据项目之电商数仓(用户行为数据)(十五)
    项目实战从0到1之hive(46)大数据项目之电商数仓(用户行为数据)(十四)
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/10105117.html
Copyright © 2020-2023  润新知