-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanfordalgo2.cpp
63 lines (63 loc) · 1.38 KB
/
stanfordalgo2.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
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
int counter;
int partitioned(int a[],int left,int right){
int median;
if(a[left] < a[right]){
if(a[left] > a[(left+right)/2])
median = left;
else if(a[right] < a[(left+right)/2])
median = right;
else
median = (left+right)/2;
}
else{
if(a[right] > a[(left+right)/2])
median = right;
else if(a[left] < a[(left+right)/2])
median = left;
else
median = (left+right)/2;
}
swap(a[left],a[median]);
int pivot = left,i = left+1,j;
while(i < right+1){
if(a[i] > a[pivot])
break;
i++;
}
j = i;
while(1){
while(j < right+1){
if(a[j] < a[pivot])
break;
j++;
}
if(j == right+1)
break;
swap(a[i],a[j]);i++;j++;
}
swap(a[pivot],a[i-1]);
return i-1;
}
void quicksort(int a[],int left,int right){
int p;
if(left < right){
p = partitioned(a,left,right);
quicksort(a,left,p-1);
quicksort(a,p+1,right);
counter += right-left;
}
}
int main(){
int a[10001],i = 0;
string c;
ifstream f("QuickSort.txt");
getline(f,c,'\n');
while(!f.eof()){
a[i++] = stoi(c);
getline(f,c,'\n');
}
quicksort(a,0,9999);
cout<<counter;
}