Ultra-QuickSort
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ultra-QuickSort相关的知识,希望对你有一定的参考价值。
Description
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.
Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6
代码提交结果:compile error
困惑,不知道为什么
#include <stdio.h>
#include <stdlib.h>
void merging(int *list1,int list1_size,int *list2,int list2_size,int num,int *count)
{
int i,j,k,m;
i=j=k=0;
int temp[num],a,b;
while (i<list1_size && j<list2_size) {
a=list1[i];
b=list2[j];
if(list1[i]<list2[j])
{
temp[k++]=list1[i++];
}else{
temp[k++]=list2[j++];
(*count)=(*count)+list1_size-i;
}
}
while (i<list1_size) {
temp[k]=list1[i];
k++;
i++;
}
while (j<list2_size) {
temp[k]=list2[j];
k++;
j++;
}
for (m=0; m<list1_size+list2_size; m++) {
list1[m]=temp[m];
}
}
void sort(int *p,int num,int *count)
{
if (num>1) {
int *list1=p;
int list1_size=num/2;
int *list2=p+num/2;
int list2_size=num-list1_size;
sort(list1, list1_size,count);
sort(list2, list2_size,count);
merging(list1,list1_size,list2,list2_size,num,count);
}
}
int main() {
int num;
int *p;
int i;
int *count;
int result;
while (scanf("%d",&num)&&num!=0) {
result=0;
count=&result;
p=(int *)malloc(num*sizeof(int));
for (i=0; i<num; i++) {
scanf("%d",&p[i]);
}
sort(p,num,count);
printf("%d\n",(*count));
}
return 0;
}
0
以上是关于Ultra-QuickSort的主要内容,如果未能解决你的问题,请参考以下文章
D - Ultra-QuickSort (POJ - 2299)