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的更多相关文章

  1. quickSort算法导论版实现

    本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...

  2. Javascript算法系列之快速排序(Quicksort)

    原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...

  3. JavaScript 快速排序(Quicksort)

    "快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...

  4. QuickSort 快速排序 基于伪代码实现

    本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...

  5. quicksort

    快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...

  6. 随手编程---快速排序(QuickSort)-Java实现

    背景 快速排序,是在上世纪60年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...

  7. Teleport Ultra 下载网页修复

    1 三个基本正则替换 tppabs="h[^"]*"/\*tpa=h[^"]*/javascript:if\(confirm\('h[^"]*[Ult ...

  8. Ultra Video Splitter & Converter

    1. Video Splitter http://www.aone-soft.com/splitter.htm Ultra Video Splitter 是一款视频分割工具.可将一个巨大的AVI/Di ...

  9. 算法实例-C#-快速排序-QuickSort

    算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...

  10. mac 激活Ultra Edit16

    一.文本编辑器UltraEdit 参照Ultra Edit16.10 Mac 破解下载,或者官方下载 Ultra Edit16即可 printf of=/Applications/UltraEdit. ...

随机推荐

  1. mysql中数据类型的取值范围

    mysql整型bigint.int.mediumint.smallint 和 tinyint的语法介绍,如下: 1.bigint 从 -2^63 (-9223372036854775808) 到 2^ ...

  2. windows server 开机自动登录并锁定

    这个操作对于广大使用Windows(包括xp/win7/2003/2008 R2 等windows 系统) 的上班族会有点用. 其一:如果是个人吧系统(win7.xp)上班时候打开电脑,自动登录,系统 ...

  3. redmine + apache + mod_fcgid

    redmine默认是用webrick启动的,这种方法不适合生产环境,最好部署在apache下,本文介绍如何通过mod_fcgid启动. 首先要有一个能够启动的redmine,可以通过我之前的博文查看: ...

  4. 你离月薪30K还差哪些?

    这类标题的文章,是不是很熟悉?你是不是冲着标题进来的? 类似这样的标题党文章,你应该看过很多,多数是泛泛而谈,没啥用- 今天老徐跟大家用几个真实案例,聊点有用的- 看完之后,你至少知道自己的差距是哪些 ...

  5. ios界面布局整理

    1 UIView 1.1 创建自定义的UIView的xib文件 [1]设置 file's Owner的 Custom Class 中的class 执行自定义控件类 [2]设置当前UIView 的屏幕大 ...

  6. mysql5.6主从参数详解

    mysql5.6的主从相当的不错,增加了不少参数,提升了主从同步的安全和效率,以下是mysql5.6主从参数详解. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  7. 【转】RHadoop实践系列之二:RHadoop安装与使用

    RHadoop实践系列之二:RHadoop安装与使用 RHadoop实践系列文章,包含了R语言与Hadoop结合进行海量数据分析.Hadoop主要用来存储海量数据,R语言完成MapReduce 算法, ...

  8. Linux命令行上传文件到百度网盘

    利用bpcs_uploader你可以自动将VPS主机上的文件上传到百度网盘中,同时也可以从百度网盘中下载文件到VPS主机上,让你的文件安全地"住"在百度云中.[font=Tahom ...

  9. 配置安装DVWA

    本文地址:http://www.cnblogs.com/go2bed/p/4162313.html —————————————————— 什么是DVWA? Damn Vulnerable Web Ap ...

  10. Nagios check_logfiles插件的使用记录

    1 获取与安装 https://labs.consol.de/assets/downloads/nagios/check_logfiles-3.7.4.tar.gz 链接可能会失效,建议去官网下载. ...