forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquickSort_Last_Pivot.cpp
57 lines (53 loc) · 1.03 KB
/
quickSort_Last_Pivot.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
54
55
56
57
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
return;
}
int partition(int arr[], int start, int end)
{
int pivot = arr[end];
int i = start - 1;
for (int j = start; j < end; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(arr[i], arr[j]);
}
}
i++;
swap(arr[i], arr[end]);
return i;
}
void quickSort(int arr[], int start, int end)
{
if (start < end)
{
int part = partition(arr, start, end);
quickSort(arr, start, part - 1);
quickSort(arr, part + 1, end);
}
}
int main()
{
int size;
cout << "Enter size: ";
cin >> size;
int arr[size];
cout << "Enter array: ";
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
quickSort(arr, 0, size - 1);
cout << "Sorted array is: ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}