-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort1.cpp
More file actions
46 lines (38 loc) · 865 Bytes
/
QuickSort1.cpp
File metadata and controls
46 lines (38 loc) · 865 Bytes
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
38
39
40
41
42
43
44
45
46
//choose lower index as pivort
#include<bits/stdc++.h>
using namespace std;
int getPivort(int arr[],int low, int high){
int i = low;
int j = high;
int pivort = arr[low];
while(i<j){
while(arr[i]<=pivort && i<high) i++;
while(arr[j]>pivort && j>=low) j--;
if(i<j) swap(arr[i],arr[j]);
}
swap(arr[j],arr[low]);
return j;
}
void quickSort(int arr[], int low,int high){
if(high>low){
int pivort = getPivort(arr,low,high);
quickSort(arr,low,pivort-1);
quickSort(arr,pivort+1,high);
}
}
void printArr(int arr[],int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
int main(){
// freopen("input.txt","r",stdin);
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
quickSort(arr,0,n-1);
printArr(arr,n);
}