Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
终于用PASCAL过了这道题后的一些经验。这道题目可以用堆排序或快速排序来做。 我是用快排作的,关键是排序前需要判断是否已经有序。 郁闷的是用随机居然速度还慢。 Procedure qt(s,t:longint); var i,j,x,y:longint; begin pk:=true; for i:=s to t-1 do if da[i]>da[i+1] then begin pk:=false; break end; if pk then exit; i:=s; j:=t;{1} x:=da[i];{2} {y:=s+trunc(random(t-s)); if y>t then y:=t; x:=da[y]; da[y]:=da[i]; da[i]:=x;} while i<j do begin while (da[j]>=x) and (j>i) do j:=j-1; if j>i then begin da[i]:=da[j]; i:=i+1 end; while (da[i]<=x) and (i<j) do i:=i+1; if i<j then begin da[j]:=da[i]; j:=j-1 end; da[i]:=x end; if s<(i-1) then qt(s,i-1); if (i+1)<t then qt(i+1,t); end; Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator