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. 使用Gradle构建构建一个Java Web工程及持续集成环境Jenkins配置

    安装Eclipse插件——Buildship 什么是Buildship? Buildship能方便我们通过Eclipse IDE创建和导入Gradle工程,同时还能执行Gradle任务. Eclips ...

  2. input 默认值为灰色,输入时清楚默认值

    input 默认值为灰色,输入时清楚默认值 <input value="please input your name" onFocus="if(value==def ...

  3. HTML5 review

    标签: section:适用于独立结构内容,无结构关系(例如article,aside与其他标签可以组合形成具有特定结构关系的标签结构).须具有标题(待考证). menu&nav:menu用于 ...

  4. Review 代码

    最近需要 Review 代码,学习了<代码整洁之道>.<代码质量>等书籍. 把对这些代码之道的学习心得整理成文

  5. T-SQL性能调整(一)--编译和重新编译

    概念简介 我们平时所说的查询在SQLServer 中主要有两部分来实现: 编译查询,主要包括了五个环节(缓存查找.分析.代数化.优化.缓存新计划) 流程描述: 首先,在计划缓存中查找是否包含新的查询, ...

  6. SQL Server 2000:提示“未与信任SQL SERVER连接相关连”错误

    在使用“用户模式”登陆SQL Server 2000时提示“未与信任SQL SERVER连接相关连”错误,因为在安装SQL Server时选择“仅Windows”模式,所以所有用户都不可以登陆. 解决 ...

  7. 第二题 已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西 兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利

    package com.hanqi.test; import java.util.ArrayList; import java.util.List; import java.util.Random; ...

  8. c#-轮询算法

    这两天做东西,业务上有个特殊的需求,在用户访问页面的时候,针对某一行代码进行控制,按照概率来进行显示,我做的是针对当前页面的曝光进行处理,曝光代码是第三方的,页面上只要有这段代码就算是执行了这段曝光代 ...

  9. 烂泥:centos单独编译安装gd库

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天一直在弄一个商城系统,该系统的源码及数据库都已经上传并创建完毕.但是在安装该系统时,却提示缺少gd库.如下: 使用php探针查看,发现php确实没 ...

  10. dyld: Library not loaded: @rpath/libswiftCore.dylib 解决方法

    解决: 设置Build Setting - > 搜索 embe关键字 -> 修改属性 见如下图: 如果更新了Xcode 8 这里变成: