-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
50 lines (45 loc) · 886 Bytes
/
QuickSort.cpp
File metadata and controls
50 lines (45 loc) · 886 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
#include <iostream>
#include <fstream>
using namespace std;
void quicksort(int *arr, int low, int high);
int split(int *arr, int low, int high);
int main() {
ifstream fin("QuickSort.in");
ofstream fout("QuickSort.out");
int n, *arr;
fin >> n;
arr = new int[n];
for(int i=1; i<=n; i++)
fin >> arr[i];
quicksort(arr, 1, n);
for(int i=1; i<n; i++)
fout << arr[i] << " ";
fout << arr[n] << endl;
delete[] arr;
return 0;
}
void quicksort(int *arr, int low, int high) {
if(low<high) {
int w = split(arr, low, high);
quicksort(arr, low, w-1);
quicksort(arr, w+1, high);
}
}
int split(int *arr, int low, int high) {
int i=low;
int x=arr[i];
for(int j = low+1; j<=high; j++) {
if (arr[j]<=x) {
i++;
if(i!=j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int temp = arr[low];
arr[low] = arr[i];
arr[i] = temp;
return i;
}