/* Bismillahir Rahmanir Rahim
Implementation of Quick-Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;
int ar[1000];
int partition (int low, int high){
int pivot=ar[high];
int i=(low-1);
for(int j=low; j<high; j++){
if(ar[j]<=pivot){
i++;
swap(ar[i],ar[j]);
}
}
swap(ar[i+1], ar[high]);
return (i+1);
}
void quickSort(int low, int high){
if(low<high){
int pi=partition(low, high);
quickSort(low, pi-1);
quickSort(pi+1, high);
}
}
int main(){
int n,i;
cin>>n;
for(i=0;i<n;i++) cin>>ar[i];
quickSort(0, n-1);
for(i=0; i<n; i++) cout<<ar[i]<<" ";
cout<<endl;
return 0;
}
|
Monday, April 24, 2017
Quick Sort - Implementation with C++
Subscribe to:
Post Comments (Atom)
-
#include<bits/stdc++.h> #define ll long long using namespace std ; ll n , k , t_case ; ll bigmod ( ll b , ll p , ll m...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3...
No comments:
Post a Comment