-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
47 lines (47 loc) · 1.11 KB
/
quick_sort.cpp
File metadata and controls
47 lines (47 loc) · 1.11 KB
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
#include<iostream>
using namespace std;
int partition(int arr[],int start, int end){
int pivot = arr[start];
int count = 0;
for(int i = start + 1; i <= end; i++){
if(arr[i] <= pivot){
count++;
}
}
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
int i = start ;
int j = end;
while(i < pivotIndex && j > pivotIndex){
while(arr[i] <= pivot){
i++;
}
while(arr[j] > pivot){
j--;
}
if(i < pivotIndex && j > pivotIndex){
swap(arr[i], arr[j]);
i++;
j--;
}
}
return pivotIndex;
}
void quicksort(int arr[], int start, int end){
if(start >= end){
return;
}
int p = partition(arr,start, end);
quicksort(arr, start, p-1);
quicksort(arr, p+1, end);
}
int main(){
int arr[] = {10,9,8,7,6,5,4,3,2,1};
int n = sizeof(arr)/sizeof(arr[0]);
quicksort(arr, 0, n-1);
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
return 0;
}