题解 | #排序#

排序

https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef

#include<iostream>
#include<cstring>
using namespace std;
int arr[101];
int Partition(int A[],int low,int high){
    A[0]=A[low];
    while(low<high){
        while(low<high && A[high]>=A[0])--high;
        A[low]=A[high];
        while(low<high && A[low]<=A[0]) ++low;
        A[high]=A[low];
    }
    A[low]=A[0];
    return low;
    

}
void QuickSort(int A[],int low,int high){
    if(low<high){
        int pivotpos=Partition(A,low,high);
        QuickSort(A, low,pivotpos-1);
        QuickSort(A,pivotpos+1,high);
    }


}
int main(){
    int n;
    while(cin>>n && n){
        memset(arr,0,101);
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        QuickSort(arr, 1, n);
        for(int i=1;i<=n;i++){
            cout<<arr[i]<<" ";
        }
    }

}

全部评论

相关推荐

10-16 15:48
算法工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务