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. Linux双机信任,适用统一安装

    一.生成建立安全信任关系的证书. 在A机root用户下执行ssh-keygen命令,在需要输入的地方,直接回车, # ssh-keygen -t rsa 注:直接回车就行 二.查看生成密钥的文件 # ...

  2. php设计模式 数据对象映射模式

    数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作. 在代码中实现数据对象映射模式,实现一个ORM类,将复杂的sql语句映射成对象属性的操作.对象关系映射(Obje ...

  3. 安卓普通类通过classloader访问资源文件

    Android studio不知道怎么设置,才可以在生成APK时把一些文件打包进去. 但是不管怎么样,放在res文件夹下的东西是一定得打包的.所以把一些资源文件放在res/raw这个文件夹里是科学的. ...

  4. 用MsmqBinding投送message出现的一个灵异事件 【第二篇】

    一直都在用Msmqbinding,也一直忽视了message里面的内容格式是什么样的,这也是微软给我们高层封装带给我们的开发效率,但同时一旦中间出了什么问题, 就不知道从何查起了.有个需求是这样的,服 ...

  5. 【转】input输入框中光标高度的变化问题

    原文地址:http://blog.csdn.net/luochao_tj/article/details/17755457 input[type='text']文本框光标高度在有输入内容和为空时发生很 ...

  6. db2服务端安装图解

    一. 准备工作 1. db2服务端安装包,版本:10.1.2 二. 安装图解过程 1. 响应文件是一个包含安装和配置信息的纯英文文本文件.可无需任何用户交互进行db2的批量安装.非必须的. 2. 点击 ...

  7. Linux基础命令之cat使用方法大全

    今天在学习部署安装openstack的时候,看到一个关于cat的奇怪用法,可能是本人的才疏学浅没见过这种写法,于是乎查阅资料了一番,并进行了总结,希望也能够帮助有需要的朋友. 以下是我总结的几种常用方 ...

  8. /proc/interrupts 统计2.6.38.8与3.10.25差异

    eth4进,eth5出 linux-3.10.25 67:          2          3          2          3   PCI-MSI-edge     eth468: ...

  9. HTTP 请求报文 响应报文

    引言 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发 ...

  10. AI (Adobe Illustrator)详细用法(五)

    最后的调整和输出. 一.改变形状工具/宽度工具/包裹工具 1.改变形状工具[整形工具] 改变形状工具可以让我们更细致的控制形状的改变. 用钢笔工具画一条曲线,并设置宽度样式等. 如果想让这条曲线形状变 ...