var n,i:longint; a,temp:array[1..100000] oflongint;{不要忘了設temp數組,要不然很悲劇} procedure merge(l,m,r:longint); var i,j,k:longint; begin i:=l; j:=m+1; for k:=l to r do begin if(i<=m) and ((j>r)or(a[i]<=a[j])) then begin temp[k]:=a[i]; inc(i); end else begin temp[k]:=a[j]; inc(j); end; end; for k:=l to r do a[k]:=temp[k]; end; procedure sort(l,r:longint); var m:longint; begin m:=(l+r) shr 1; if l<>r then begin sort(l,m); sort(m+1,r); merge(l,m,r); end; end; begin ans:=0; read(n); for i:=1 to n do read(a[i]); sort(1,n); for i:=1 to n do write(a[i],' '); end.
歸併排序{nlogn}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.