diff options
author | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2016-11-19 19:00:47 +0700 |
---|---|---|
committer | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2016-11-19 20:46:04 +0700 |
commit | f97ad3e9e225a288e7a8a53c2ef0acb45a47dbde (patch) | |
tree | 4d2d7ff6fd480396ed862069e7edc26fa7b66ccd /12/TP-ThanhHoá-2009/sortnfind.pas | |
parent | 10de10a507238f3be43dee304e057679b5bb2736 (diff) | |
download | cp-f97ad3e9e225a288e7a8a53c2ef0acb45a47dbde.tar.gz |
Thêm đề 12 Thanh Hoá 2008-2009
Diffstat (limited to '12/TP-ThanhHoá-2009/sortnfind.pas')
-rw-r--r-- | 12/TP-ThanhHoá-2009/sortnfind.pas | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/12/TP-ThanhHoá-2009/sortnfind.pas b/12/TP-ThanhHoá-2009/sortnfind.pas new file mode 100644 index 0000000..52e8d6b --- /dev/null +++ b/12/TP-ThanhHoá-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. |