Ultra-QuickSort

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ultra-QuickSort相关的知识,希望对你有一定的参考价值。

Description

技术分享In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,

Ultra-QuickSort produces the output 
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

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的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2299 Ultra-QuickSort

POJ2299 Ultra-QuickSort

POJ2299 Ultra-QuickSort

D - Ultra-QuickSort (POJ - 2299)

POJ-2299 Ultra-QuickSort(用树状数组求逆序对数)

Ultra-QuickSort