-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort.cpp
53 lines (41 loc) · 1.05 KB
/
Quicksort.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 quicksort(int arr[], int left, int right) {
if (left < right) {
int pivot = arr[left];
int i = left + 1;
int j = right;
while (i <= j) {
while (i <= j && arr[i] < pivot) {
i++;
}
while (i <= j && arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr[i], arr[j]);
i++;
j--;
}
}
swap(arr[left], arr[j]);
quicksort(arr, left, j - 1);
quicksort(arr, j + 1, right);
}
}
int main() {
int arr[] = {6,45,89,25,61,78,24,35};
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"Unsorted Array is ";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
quicksort(arr, 0, n - 1);
cout<<"Sorted array is : ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout<<endl;
return 0;
}