From 1a8e1e68759611a66ae116771da5f376c95a3b9f Mon Sep 17 00:00:00 2001 From: Raphael McSinyx Date: Sun, 12 Feb 2017 16:18:20 +0700 Subject: Update others/volume1 --- others/volume1/sortnfind.pas | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 others/volume1/sortnfind.pas (limited to 'others/volume1/sortnfind.pas') diff --git a/others/volume1/sortnfind.pas b/others/volume1/sortnfind.pas new file mode 100644 index 0000000..a65cde0 --- /dev/null +++ b/others/volume1/sortnfind.pas @@ -0,0 +1,82 @@ +unit sortnfind; + +interface + + type + intar = array of int64; + + procedure qsort(var a : intar); + + function binin( + a: intar; + x: int64 + ): boolean; + +implementation + + procedure qsort(var a : intar); + procedure sort(l, r: integer); + var + i, j, x, y: integer; + + 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: int64 + ): boolean; + + var + l, h, mid: uint64; + + 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