zoukankan      html  css  js  c++  java
  • 快速排序

    要求:先输入n, 表示需要排n个数。

    #include<stdio.h>
    int a[101], n;
    void quicksort(int left, int right)
    {
        int i, j, t, temp;
        if(left>right)      /*若满足此条件,表明已将基准数放到该放的位置,需要返回上一层*/
            return ;        
    
    
        temp = a[left]; /*temp代表基准数*/
        i = left;
        j = right;
        while(i != j)
        {
            while(a[j] >= temp && j > i)
                j--;
            while(a[i] <= temp && j > i)
                i++;
            if(i < j)
            {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
    
        }
        a[left] = a[i];
        a[i] = temp;        /*将基准数放到应该放的位置*/
    
        /*以下为递归*/
    
        quicksort(left, i-1); /*排基准数左边的部分*/
        quicksort(i+1, right);   /*排基准数右边的部分*/
    
    }
    int main()
    {
        scanf("%d",&n);
        int i;
        for(i = 0; i < n; i++)
            scanf("%d",&a[i]);
        printf("i = %d
    ",i);
        quicksort(0, n-1);         /*0 表示 最左边位置, n-1 表示 最右边位置*/
    
        for(i = 0; i < n ;i++)
            printf("%d ",a[i]);
        return 0;
    }
    
    
  • 相关阅读:
    Demo
    Demo
    z-yelir-~
    CSP考前总结
    NOIP刷题
    清北学堂
    qsing
    【csp模拟赛九】--dfs3
    【csp模拟赛九】--dfs2
    【csp模拟赛九】--dfs
  • 原文地址:https://www.cnblogs.com/KeepZ/p/11143810.html
Copyright © 2011-2022 走看看