-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort2.cpp
More file actions
63 lines (48 loc) · 1023 Bytes
/
Copy pathquicksort2.cpp
File metadata and controls
63 lines (48 loc) · 1023 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
55
56
57
58
59
60
61
62
63
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void partition(int *ar, int first, int last, int &pivotIndex) {
int pivot = ar[first];
int i=first, j=first+1;
for(; j<=last; j++)
{
if(ar[j] < pivot )
{
i++;
int temp = j;
int value = ar[j];
for(; temp>i; temp-- )
swap(ar[temp] , ar[temp-1]);
}
}
pivotIndex = i;
for(int k=first; k<i; k++)
swap(ar[k], ar[k+1]);
}
void quickSort(int* ar, int first, int last) {
int pivotIndex;
if (first < last) {
partition(ar, first, last, pivotIndex);
quickSort(ar, first, pivotIndex-1);
quickSort(ar, pivotIndex+1, last);
}
if(last > first)
{
for(int i=first; i<=last; i++)
cout<<ar[i]<<" ";
cout<<endl;
}
}
int main() {
int _ar_size;
cin >> _ar_size;
int array[_ar_size];
for(int _ar_i=0; _ar_i<_ar_size; _ar_i++) {
int _ar_tmp;
cin >> _ar_tmp;
array[_ar_i] = _ar_tmp;
}
quickSort(array, 0, _ar_size-1);
return 0;
}