Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <bitset>
- #include <cstring>
- #include <cctype>
- #include <cmath>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <string>
- #include <set>
- #include <stack>
- #include <stdio.h>
- #include <sstream>
- #include <utility>
- #include <vector>
- #define INT_MAX 2147483647
- #define INT_MIN -2147483648
- #define pi acos(-1.0)
- #define N 1000000
- #define LL long long
- using namespace std;
- int comparisonCount = 0;
- int chooseMedianOfThreeAsPivot(int a[],int start, int end) {
- int pivotIndex = -1;
- int mid = ((end - start) / 2) + start;
- int b[3];
- b[0] = a[start];
- b[1] = a[mid];
- b[2] = a[end];
- sort(b,b+3);
- if (b[1] == a[start]) {
- pivotIndex = start;
- }else if(b[1] == a[mid]) {
- pivotIndex = mid;
- }else {
- pivotIndex = end;
- }
- // Move pivot to start index.
- int temp = a[start];
- a[start] = a[pivotIndex];
- a[pivotIndex] = temp;
- return start;
- }
- int partition(int a[],int l,int r,int pivotIndex)
- {
- int arrayLength = r - l;
- comparisonCount += arrayLength;
- int p,i,j;
- p = a[pivotIndex];
- i = l+1;
- for(j=l+1;j<=r;j++)
- {
- if(a[j]<p)
- {
- int t = a[i];
- a[i] = a[j];
- a[j] = t;
- i++;
- }
- }
- int t=a[l];
- a[l] = a[i-1];
- a[i-1] = t;
- return i-1;
- }
- void quick_sort(int *a,int l,int r)
- {
- if (r <= l) {
- return;
- }
- if(l<r)
- {
- int pivotIndex = chooseMedianOfThreeAsPivot(a,l,r);
- int s = partition(a,l,r,pivotIndex);
- quick_sort(a,l,s-1);
- quick_sort(a,s+1,r);
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("data.txt", "r", stdin);
- freopen("output.txt","w",stdout);
- #endif
- int array[100005];
- for(int i=0;i<10000;i++)
- cin>>array[i];
- // partition(array,1,10000);
- quick_sort(array,0,9999);
- for(int i=0;i<10000;i++)
- cout<<array[i]<<"\n";
- cout<<"\n\n\n";
- cout<<comparisonCount<<"\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement