Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

谁帮忙看一下程序?我用的是快速排序,超时了……先谢谢了!

Posted by yufuwan1 at 2008-05-31 04:14:54 on Problem 2388
#include<stdio.h>

int partition(long int r[],int low,int high)
{
	int pivotkey;
	r[0]=r[low];
	pivotkey=r[low];
	while(low<high)
	{
		while((low<high)&&(r[high]>pivotkey)){--high;}
		r[low]=r[high];
		while((low<high)&&(r[low]<pivotkey)){++low;}
		r[high]=r[low];
	}
	r[low]=r[0];
	return low;
}

void QSort(long int r[],int low,int high)
{
	int pivotloc;
	if(low<high)
	{
		pivotloc=partition(r,low,high);
		QSort(r,low,pivotloc-1);
		QSort(r,pivotloc+1,high);
	}
}

void QuickSort(long int r[],int n)
{
	QSort(r,1,n);
}
	

int main()
{
	int n;
	int i;
	long int a[10001];
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	QuickSort(a,n);
	printf("%ld",a[(n+1)/2]);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator