• 交换排序


    #include "stdio.h"
    #define MAXSIZE 20
    typedef int ElemType;
    typedef struct
    {
        ElemType r[MAXSIZE+1];
        int length;
    }SortList;
    
    /*
    冒泡排序:BubbleSort
    */
    
    void BubbleSort(SortList *L)
    {
        int i,j;ElemType t;
        for(i=1;i<L->length;i++)            //排序趟数
            for(j=1;j<=L->length-i;j++)        //每趟比较的次数
                if(L->r[j]>L->r[j+1])
                {
                    t = L->r[j];
                    L->r[j] = L->r[j+1];
                    L->r[j+1] = t;
                }
    }
    
    //改进算法
    void BubbleSort(SortList *L)
    {
        int i,j; ElemType t;
        int flag = 1;
        for(i=1;flag&&i<L->length;i++)
            for(flag=0,j=1;j<=L->length-i;j++)
                if(L->r[j]>L->r[j+1])
                {
                    t = L->r[j];
                    L->r[j] = L->r[j+1];
                    L->r[j+1] = t;
                    flag = 1;                    //有交换,则置交换标记为真,需要进行下趟排序
                }
    }
    
    
    
    //快速排序
    void HoareSort(SortList *L,int low,int high)
    {
        int i,j;
        if(low<high)
        {
            i = low; j = high;
            L->r[0] = L->r[i];
            while(i<j)
            {
                while(i<j&&L->r[j]>=L->r[0])j--;    
                L->r[i] = L->r[j];
                while(i<j&&L->r[i]<L->r[0])i++;
                L->r[j]=L->r[i];
            }
            L->r[i]=L->r[0];
            HoareSort(L,low,i-1);
            HoareSort(L,i+1,high);
        }
    }
    void QuickSort(SortList *L)
    {
        HoareSort(L,1,L->length);
    }
  • 相关阅读:
    塔吊滑车移动
    csv合并某几列
    合并两个csv
    QFile
    python alphashape
    pip install econml
    csv增加一列
    连接redis数据库日志
    yolov5训练识别钢筋模型
    redis查看某个key的类型
  • 原文地址:https://www.cnblogs.com/wzqstudy/p/10213175.html
Copyright © 2020-2023  润新知