Ultra-QuickSort produces the output
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
5 9 1 0 5 4 3 1 2 3 0Sample Output
6 0
归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
首先考虑下如何将将二个有序数列合并。这个非常简单,只要从比较二个数列的第一个数,谁小就先取谁,取了后就在对应数列中删除这个数。然后再进行比较,如果有数列为空,那直接将另一个数列的数据依次取出即可。
- //将有序数组a[]和b[]合并到c[]中
- void MemeryArray(int a[], int n, int b[], int m, int c[])
- {
- int i, j, k;
- i = j = k = 0;
- while (i < n && j < m)
- {
- if (a[i] < b[j])
- c[k++] = a[i++];
- else
- c[k++] = b[j++];
- }
- while (i < n)
- c[k++] = a[i++];
- while (j < m)
- c[k++] = b[j++];
- }
可以看出合并有序数列的效率是比较高的,可以达到O(n)。
解决了上面的合并有序数列问题,再来看归并排序,其的基本思路就是将数组分成二组A,B,如果这二组组内的数据都是有序的,那么就可以很方便的将这二组数据进行排序。如何让这二组组内数据有序了?
可以将A,B组各自再分成二组。依次类推,当分出来的小组只有一个数据时,可以认为这个小组组内已经达到了有序,然后再合并相邻的二个小组就可以了。这样通过先递归的分解数列,再合并数列就完成了归并排序。
- //将有二个有序数列a[first...mid]和a[mid...last]合并。
- void mergearray(int a[], int first, int mid, int last, int temp[])
- {
- int i = first, j = mid + 1;
- int m = mid, n = last;
- int k = 0;
- while (i <= m && j <= n)
- {
- if (a[i] <= a[j])
- temp[k++] = a[i++];
- else
- temp[k++] = a[j++];
- }
- while (i <= m)
- temp[k++] = a[i++];
- while (j <= n)
- temp[k++] = a[j++];
- for (i = 0; i < k; i++)
- a[first + i] = temp[i];
- }
- void mergesort(int a[], int first, int last, int temp[])
- {
- if (first < last)
- {
- int mid = (first + last) / 2;
- mergesort(a, first, mid, temp); //左边有序
- mergesort(a, mid + 1, last, temp); //右边有序
- mergearray(a, first, mid, last, temp); //再将二个有序数列合并
- }
- }
- bool MergeSort(int a[], int n)
- {
- int *p = new int[n];
- if (p == NULL)
- return false;
- mergesort(a, 0, n - 1, p);
- delete[] p;
- return true;
- }
归并排序的效率是比较高的,设数列长为N,将数列分开成小数列一共要logN步,每步都是一个合并有序数列的过程,时间复杂度可以记为O(N),故一共为O(N*logN)。因为归并排序每次都是在相邻的数据中进行操作,所以归并排序在O(N*logN)的几种排序方法(快速排序,归并排序,希尔排序,堆排序)也是效率比较高的。
在本人电脑上对冒泡排序,直接插入排序,归并排序及直接使用系统的qsort()进行比较(均在Release版本下)
对20000个随机数据进行测试:
对50000个随机数据进行测试:
再对200000个随机数据进行测试:
注:有的书上是在mergearray()合并有序数列时分配临时数组,但是过多的new操作会非常费时。因此作了下小小的变化。只在MergeSort()中new一个临时数组。后面的操作都共用这一个临时数组
本题代码:
#include <cstdio>
#include <iostream>
using namespace std;
long long n,ans;//注意这里ans要用long long
int a[500005];
void merge(int l,int mid,int r){
int i = l,k = l,j = mid+1;
int c[500005];
while(i <= mid && j <= r){
if (a[i] > a[j]){
ans += j - k;
c[k++] = a[j++];
}else c[k++] = a[i++];
}
while(i <= mid) c[k++] = a[i++];
while(j <= r) c[k++] = a[j++];
for (int i = l;i <= r;i++) a[i] = c[i];
}
void mergeSort(int l,int r){
if (l < r){
int mid = (l + r) / 2;
mergeSort(l,mid);
mergeSort(mid+1,r);
merge(l,mid,r);
}
}
int main(){
while(cin >> n && n){
ans = 0;
for (int i = 0;i < n;i++){
cin >> a[i];
}
mergeSort(0,n-1);
printf("%lld\n",ans);
}
return 0;
}
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n;
const int N = 500005;
int a[N],temp[N];
long long ans,num;
void merge(int l,int mid,int r){
int i = l,j = mid + 1,k = 0;
num = 0;
while(i <= mid && j <= r){
if (a[i] > a[j]){
num ++;//如果a[j] < a[i],则a[j] < a[i+1];
temp[k++] = a[j++];
}else{
ans += num;//num为(mid,r]中比a[i]小的数的数目
temp[k++] = a[i++];
}
}
while(i <= mid) temp[k++] = a[i++],ans += num;
while(j <= r) temp[k++] = a[j++];
for (int i = l,j = 0;i <= r;i++,j++){
a[i] = temp[j];
}
}
void mergeSort(int l,int r){
if (l >= r) return;
int mid = (l + r) / 2;
mergeSort(l,mid);
mergeSort(mid + 1,r);
merge(l,mid,r);
}
int main(){
while(~scanf("%d",&n) && n){
for (int i = 0;i < n;i++){
scanf("%d",&a[i]);
}
ans = 0;
mergeSort(0,n-1);
printf("%lld\n",ans);
}
return 0;
}