十个数进行快排

    科技2024-12-08  41

    #include<iostream> using namespace std;

    void quicksort(int a[],int low,int high) {     int temp;     int i=low,j=high;     if(low<high)     {         temp=a[low];         //下面这个循环进行了一趟比较,小于temp的关键字放在左边,大于temp的关键字放在右边         while(i<j)         {             while(j>i&&a[j]>=temp)                 --j;             if(i<j)             {                 a[i]=a[j];                 ++i;             }             while(i<j&&a[i]<temp)                 ++i;             if(i<j)             {                 a[j]=a[i];                 --j;             }         }         a[i]=temp;         quicksort(a,low,i-1);         quicksort(a,i+1,high);     } }

    int main() {     int i,j;     int a[10];     cout<<"请输入十个数值"<<endl;     for(i=0;i<10;i++)         cin>>a[i];     quicksort(a,0,9);     for(i=0;i<10;i++)         cout<<a[i]<<"  ";     cout<<endl;     return 0; }

    Processed: 0.010, SQL: 8