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

Re:莫名其妙,用sort超时,自己编个冒泡就过了.

Posted by sunmoonstar_love at 2006-12-26 23:00:32 on Problem 2487
In Reply To:莫名其妙,用sort超时,自己编个冒泡就过了. Posted by:desertseek at 2006-12-26 22:10:32
老师写的程序

template<typename Bitr>
Bitr partition(Bitr first, Bitr last)
{   //simple version, *first as pivot, guarded
	//precondition:[first, last) not empty
	Bitr ppivot = first;
	while(true)
	{
		while(*ppivot < *--last)
			;
		while((first != last)&&!(*ppivot < *++first))
			//while(first != last && *++first <= *ppivot)
			//*++first <= *ppivot
			;
		if(first == last)
		{
			std::swap(*ppivot, *last);
			return last;
		}
		else
			std::swap(*first, *last);
	}
}//~partition(Ritr first, Ritr last)
//qsort
template<typename Bitr>
void qsort(Bitr first, Bitr last)
{
	if(first == last)
		return;
	Bitr ppivot = partition<Bitr>(first, last);
	qsort(first, ppivot);
	qsort(++ppivot, last);
}//qsort(Bitr first, Bitr last) 
template<typename Bitr>
void qsort_v1r2(Bitr first, Bitr last)
{ //tail recursion removed
	while(first != last)
	{
		Bitr ppivot = partition(first, last);
		qsort_v1r2(first, ppivot);
		first = ++ppivot;
	}
}//~qsort_v1r2(Bitr first, Bitr last)
template<typename Ritr>
void qsort_v2(Ritr first, Ritr last)
{
	while(first != last)
	{
		Ritr ppivot = partition(first, last);
		if(ppivot - first < last - ppivot)
		{ //short long
			qsort_v2(first, ppivot);
			first = ppivot+1;
		}
		else
		{ //long short
			qsort_v2(ppivot+1, last);
			last = ppivot;
		}
	}//~while
}//~qsort_v2(Ritr first, Ritr last)

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