-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
54 lines (43 loc) · 1001 Bytes
/
quickSort.cpp
File metadata and controls
54 lines (43 loc) · 1001 Bytes
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
//
// Created by ladpr on 10-08-2024.
//
#include <iostream>
#include <algorithm>
int* arr;
int size;
int Partition(const int left, const int right) {
const int pivot = arr[right];
int store = left;
for (int j = left; j < right; j++) {
if (arr[j] <= pivot) {
std::swap(arr[j], arr[store]);
store++;
}
}
std::swap(arr[right], arr[store]);
return store;
}
void quickSort(const int left, const int right) {
if (left < right) {
const int mid = Partition(left, right);
quickSort(left, mid - 1);
quickSort(mid + 1, right);
}
}
int main() {
std::cout << "Enter the size of the array :";
std::cin >> size;
arr = new int[size];
for (int i = 0; i < size; i++) {
std::cout << "Enter #" << i + 1 << " element :";
std::cin >> arr[i];
}
quickSort(0, size - 1);
std::cout << std::endl << "Elements ";
for (int i = 0; i < size; i++) {
std::cout << "[" << arr[i] << "] ";
}
std::cout << std::endl;
delete[] arr;
return 0;
}