• 选择排序&快速排序


    /////////selectSort
    //cases:
    //5
    //20
    //1 45 14 23 45 15 15 13 456 1 4165 456 123 436 13 6 456 651 65 6
    //10
    //1 5 8 9 6 4 5 3 8 6 
    //30
    //1 4 2 7 1 7 8 9 2 4 1 4 2 7 1 7 8 9 2 4 1 2 3 7 4 8 5 9 4 2
    //4
    //1 2 3 8
    //5
    //1 57 8 9 6
    #include <stdio.h>
    #define SIZE 1000
    int data[SIZE];
    
    void selectSort(int n)
    {
        for(int i=0;i<n;i++)
        {
            int max = 0;
            int maxPos = 0;
            for(int j=i;j<n;j++)
            {
                if(data[j]>max)
                {
                    max = data[j];
                    maxPos = j;
                }
            }
            data[maxPos] = data[i];
            data[i] = max;
        }
    }
    
    
    
    int main(void)
    {
        //freopen("input.txt","r",stdin);
        int ncases;
        scanf("%d",&ncases);
        for(int i=0;i<ncases;i++)
        {
            int n;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&data[i]);
            }
            selectSort(n);
            for(int i=0;i<n;i++)
            {
                printf("%d ",data[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    ////////quickSort
    //case:
    //5
    //20
    //1 45 14 23 45 15 15 13 456 1 4165 456 123 436 13 6 456 651 65 6
    //10
    //1 5 8 9 6 4 5 3 8 6 
    //30
    //1 4 2 7 1 7 8 9 2 4 1 4 2 7 1 7 8 9 2 4 1 2 3 7 4 8 5 9 4 2
    //4
    //1 2 3 8
    //5
    //1 57 8 9 6
    #include <stdio.h>
    #define SIZE 1000
    int data[SIZE];
    
    
    int partition(int l, int r, int data[]){
        int i = l - 1;
        int j = r;
        int key = data[r];
    
        while(1)
        {
            while(data[++i] < key) ;
            while(data[--j] > key && j >= i) ;
            {
            if(i >= j) break;
            int tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;
            }
        }
        data[r] = data[i];
        data[i] = key;
        return i;
    }
    
    void quickSort(int l, int r, int data[]){
        if(l >= r) return;
    
        int pos = partition(l, r, data);
        quickSort(l, pos - 1, data);
        quickSort(pos + 1, r, data);
    }
    
    void quickSort(int l,int r,int data[])
    {
        if(l >= r) return;
    
        int pos = partition(l,r,data);
        quickSort(1,pos-1,data);
        quickSort(pos+1,r,data);
    }
    
    
    
    int main(void)
    {
        //freopen("input.txt","r",stdin);
        int ncases;
        scanf("%d",&ncases);
        for(int i=0;i<ncases;i++)
        {
            int n;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&data[i]);
            }
            quickSort(0,n-1,data);
            for(int i=0;i<n;i++)
            {
                printf("%d ",data[i]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    垃圾处理现状
    买了个学生阿里云114元啊,安装mysql
    4 存储器
    3 总线
    崔大哥说基础很重要
    idea使用小积累mac
    为啥要用left join on where这样的东西
    观察者模式
    从shell中退出python命令
    locust性能测试入门
  • 原文地址:https://www.cnblogs.com/xcsllll/p/6655150.html
Copyright © 2020-2023  润新知