PinkCat

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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
posted on 2016-03-30 19:06  人间君  阅读(219)  评论(0编辑  收藏  举报