-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
44 lines (42 loc) · 1.05 KB
/
quick_sort.cpp
File metadata and controls
44 lines (42 loc) · 1.05 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
#include <bits/stdc++.h>
using namespace std;
// Important part of the quick sort is the partition algorithm, it will put the pivot element in the
// right position in the array such that the element to the left of the pivot is smaller and the element to the
// right side of the array is greater.
int partition(int arr[], int first, int last)
{
int x = arr[last];
int i = first - 1;
for (int j = first; j < last; j++)
{
if (x >= arr[j])
{
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[last]);
return i + 1;
}
void quick_sort(int arr[], int first, int last)
{
if (first < last)
{
int q = partition(arr, first, last);
quick_sort(arr, first, q - 1);
quick_sort(arr, q + 1, last);
}
}
int main()
{
int arr[] = {9, 6, 0, 5, 0, 8, 2, 4, 7};
int start = 0;
int end = sizeof(arr) / sizeof(arr[0]) - 1;
quick_sort(arr, start, end);
for (int i = 0; i <= end; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}