maneedhar
10/23/2018 - 5:50 PM

quick sort

#include<bits/stdc++.h>
using namespace std;

void swap(int &x,int &y){
    int t = x;
    x = y;
    y = t;
}

int partition (vector<int> &v, int l ,int r){
    int i=l, j=l;
    while(i<r){
        if (v[i]<v[r]){
            swap(v[i],v[j]);
            j++;
        }
        i++;
    }
    swap(v[r],v[j]);
    return j;
}

void quick_sort(vector<int> &v, int l, int r){
    if (l<r){
        int x = partition (v,l,r);
        quick_sort(v,l,x-1);
        quick_sort(v,x+1,r);
    }
}

void print (vector<int>v){
    for (int i=0; i<v.size(); i++){
        cout<<v[i]<<" ";
    }
    cout<<endl;
}

int main(){
    freopen("ip.txt","r",stdin);
    freopen("op.txt","w",stdout);
    int t;
    cin>>t;
    while (t--){
        int n;
        cin>>n;
        vector<int>v(n);
        for (int i=0; i<n; i++){
            cin>>v[i];
        }
        quick_sort(v,0,n-1);
        print(v);
    }
    return 0;
}