poj2299
来源:互联网 发布:数论相似度算法 编辑:程序博客网 时间:2024/05/16 08:55
#include<stdio.h>
void quickSort(int *A,int start,int end,int *step);
void quickSort(int *A,int start,int end,int *step)
{
int i = start;
int j = end;
int temp = A[start];
if(i<j)
{
while(i<j)
{
while(i<j && temp<A[j])
{
j--;
}
if(i<j)
{
A[i++] = A[j];
(*step)++;
}
while(i<j && temp>A[i])
i++;
if(i<j)
{
A[j--] = A[i];
(*step)++;
}
}
if(i<j)
A[i] = temp;
quickSort(A,start,i-1,step);
quickSort(A,i+1,end,step);
}
}
int main(void)
{
int i;
int cnt;
int step=0;
int A[200000];
scanf("%d",&cnt);
while(cnt!=0)
{
for(i=0; i<cnt; i++)
scanf("%d",&A[i]);
quickSort(A,0,cnt-1,&step);
printf("%d",step);
step=0;
scanf("%d",&cnt);
}
}
void quickSort(int *A,int start,int end,int *step);
void quickSort(int *A,int start,int end,int *step)
{
int i = start;
int j = end;
int temp = A[start];
if(i<j)
{
while(i<j)
{
while(i<j && temp<A[j])
{
j--;
}
if(i<j)
{
A[i++] = A[j];
(*step)++;
}
while(i<j && temp>A[i])
i++;
if(i<j)
{
A[j--] = A[i];
(*step)++;
}
}
if(i<j)
A[i] = temp;
quickSort(A,start,i-1,step);
quickSort(A,i+1,end,step);
}
}
int main(void)
{
int i;
int cnt;
int step=0;
int A[200000];
scanf("%d",&cnt);
while(cnt!=0)
{
for(i=0; i<cnt; i++)
scanf("%d",&A[i]);
quickSort(A,0,cnt-1,&step);
printf("%d",step);
step=0;
scanf("%d",&cnt);
}
}
0 0
- POJ2299
- POJ2299
- poj2299
- POJ2299
- POJ2299
- poj2299
- poj2299
- poj2299
- poj2299
- poj2299
- poj2299
- poj2299
- POJ2299
- poj2299
- poj2299
- POJ2299
- poj2299
- poj2299
- 【开源库大全】十、Yalantis全系列
- ThinkPHP框架完全解析二
- mysql学习笔记之三(索引)
- 杭电1000
- 2014年上海邀请赛C Seam Carving(二维dp)
- poj2299
- 我的android第一课
- 编写可读代码的艺术(一)java&android
- mini2440硬件篇之RTC
- 小米 2016届实习生招聘笔试
- 快捷键收藏
- 工厂模式
- 作业三
- poj2388