-
Notifications
You must be signed in to change notification settings - Fork 51
/
Quick_Sort.cpp
53 lines (47 loc) · 902 Bytes
/
Quick_Sort.cpp
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
47
48
49
50
51
52
53
#include<iostream>
using namespace std;
void swap(int arr[] , int pos1, int pos2){
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
int partition(int arr[], int low, int high, int pivot){
int i = low;
int j = low;
while( i <= high){
if(arr[i] > pivot){
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}
void quickSort(int arr[], int low, int high){
if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high, pivot);
quickSort(arr, low, pos-1);
quickSort(arr, pos+1, high);
}
}
int main()
{
int n ;
cout << "Enter the size of array\n";
cin>>n;
cout << "Enter arry elements :\n";
int arr[n];
for(int i = 0 ; i < n; i++){
cin>> arr[i];
}
quickSort(arr, 0 , n-1);
cout<<"The sorted array is: ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}
}