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

    #include<cstdio>

    #include<ctime>

    #include<cstdlib>

    ===================================================== 

    void Swap(int& a,int& b){

        if(a!=b){

            a^=b;b^=a;a^=b;

        }

    }

    =====================================================

    int  Partition(int *A,int p,int r)

    {

     int x,i;

     x=A[r];

     i=p-1;

     for(int j=p;j<=r-1;++j)

     {

       if(A[j]<=x)    

            i++;//x为最后一个数,小于x, 不动(交换)本身(未有大于x的)

            Swap(A[i],A[j]);

     }

     Swap(A[++i],A[r]);

     return i;

    }

     

      //已有大于x的时,再遇小于x:交换

        //i为最后一个小于等于x元素的下标。

     

     

    =====================================================

    void  QuickSort(int *A,int p,int r)

    {

     if(p<r)

     {

      int q = Partition(A,p,r);

      QuickSort(A,p,q-1);

      QuickSort(A,q+1,r);

     }

    }

     

     

     

    #include<cstdio>  

    #include<ctime>  

    #include<cstdlib>  

      

    inline void Swap(int &a, int &b)  

    {  

        if(a!=b)  

        {  

            a^=b;  

            b^=a;  

            a^=b;  

        }  

    }  

      

    int Partition(int *A,int front,int end)  

    {  

        int key = A[end];  

        int i = front - 1;  

      

        for(int current = front;current < end;++current)  

        {  

            if(A[current]<=key)  

                Swap(A[++i],A[current]);  

        }  

        Swap(A[++i],A[end]);  

        return i;  

    }  

      

    void QuickSort (int *A,int front,int end)  

    {  

        if(front < end)  

        {  

            int midPosition = Partition(A,front,end);  

            QuickSort(A,front,midPosition-1);  

            QuickSort(A,midPosition+1,end);  

        }  

    }

     

  • 相关阅读:
    实验二、作业调度模拟实验
    实验一
    0909 初识操作系统
    实验四、主存空间的分配和回收模拟
    12.27评论5位同学试验三
    实验三进程调度模拟程序
    实验二、作业调度模拟实验
    实验一报告
    实验四 主存空间的分配和回收模拟
    实验三 进程调度模拟程序
  • 原文地址:https://www.cnblogs.com/lsx1993/p/4841481.html
Copyright © 2011-2022 走看看