From f97ad3e9e225a288e7a8a53c2ef0acb45a47dbde Mon Sep 17 00:00:00 2001 From: Raphael McSinyx Date: Sat, 19 Nov 2016 19:00:47 +0700 Subject: Thêm đề 12 Thanh Hoá 2008-2009 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "12/TP-ThanhHo\303\241-2009/sortnfind.pas" | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "12/TP-ThanhHo\303\241-2009/sortnfind.pas" (limited to '12/TP-ThanhHoá-2009/sortnfind.pas') diff --git "a/12/TP-ThanhHo\303\241-2009/sortnfind.pas" "b/12/TP-ThanhHo\303\241-2009/sortnfind.pas" new file mode 100644 index 0000000..52e8d6b --- /dev/null +++ "b/12/TP-ThanhHo\303\241-2009/sortnfind.pas" @@ -0,0 +1,83 @@ +unit sortnfind; + +interface + + type + intar = array of smallint; + + procedure qsort(var a : intar); + + function binin( + a: intar; + x: smallint + ): boolean; + +implementation + + procedure qsort(var a : intar); + + procedure sort(l, r: longint); + var + i, j, x, y: longint; + + begin + i := l; + j := r; + x := a[(l + r) div 2]; + + repeat + while a[i] < x do + inc(i); + + while x < a[j] do + dec(j); + + if i <= j then + begin + y := a[i]; + a[i] := a[j]; + a[j] := y; + inc(i); + j := j - 1 + end + until i > j; + + if l < j then + sort(l, j); + + if i < r then + sort(i, r) + end; + + begin + sort(0, length(a) - 1) + end; + + + function binin( + a: intar; + x: smallint + ): boolean; + + var + l, h, mid: word; + + begin + l := 0; + h := length(a) - 1; + + while l <= h do + begin + mid := (l + h) div 2; + if x = a[mid] then + exit(true) + else if x < a[mid] then + h := mid - 1 + else + l := mid + 1 + end; + + binin := false + end; + +end. -- cgit 1.4.1