about summary refs log tree commit diff
path: root/others/volume1/sortnfind.pas
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2017-02-12 16:18:20 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2017-02-12 16:18:20 +0700
commit1a8e1e68759611a66ae116771da5f376c95a3b9f (patch)
treee663fa6adb9264046a876e552ff2a455215c3a8b /others/volume1/sortnfind.pas
parentdf9a0d140a7b7edef66f95aa13b5bb4488147f68 (diff)
downloadcp-1a8e1e68759611a66ae116771da5f376c95a3b9f.tar.gz
Update others/volume1
Diffstat (limited to 'others/volume1/sortnfind.pas')
-rw-r--r--others/volume1/sortnfind.pas82
1 files changed, 82 insertions, 0 deletions
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.