From 8b4cdeed4896e8d2c81166b06d9a297a5cadf260 Mon Sep 17 00:00:00 2001 From: mburmistrov Date: Sun, 27 Nov 2016 23:40:45 +0300 Subject: [PATCH 1/2] Add insertion, shell, merge & quick sorts --- src/ru/mail/polis/bench/AverageTimeBench.java | 48 ++++++++++-- .../bench/ImprovedInsertionSortBench.java | 49 ++++++++++++ .../mail/polis/bench/InsertionSortBench.java | 49 ++++++++++++ src/ru/mail/polis/sort/BinaryQuickSort.java | 46 +++++++++++ src/ru/mail/polis/sort/BubbleSort.java | 3 + src/ru/mail/polis/sort/Helper.java | 76 +++++++++++++++++++ .../polis/sort/ImprovedInsertionSort.java | 17 +++++ src/ru/mail/polis/sort/InsertionSort.java | 24 ++++++ .../polis/sort/MemoryOptimisedMergeSort.java | 46 +++++++++++ src/ru/mail/polis/sort/MergeSort.java | 40 ++++++++++ src/ru/mail/polis/sort/QuickSort.java | 43 +++++++++++ .../polis/sort/RandomPivot3PartQuickSort.java | 51 +++++++++++++ src/ru/mail/polis/sort/ShellSort.java | 31 ++++++++ tests/ru/mail/polis/sort/valid/Tester.java | 44 +++++++++++ 14 files changed, 562 insertions(+), 5 deletions(-) create mode 100644 src/ru/mail/polis/bench/ImprovedInsertionSortBench.java create mode 100644 src/ru/mail/polis/bench/InsertionSortBench.java create mode 100644 src/ru/mail/polis/sort/BinaryQuickSort.java create mode 100644 src/ru/mail/polis/sort/ImprovedInsertionSort.java create mode 100644 src/ru/mail/polis/sort/InsertionSort.java create mode 100644 src/ru/mail/polis/sort/MemoryOptimisedMergeSort.java create mode 100644 src/ru/mail/polis/sort/MergeSort.java create mode 100644 src/ru/mail/polis/sort/QuickSort.java create mode 100644 src/ru/mail/polis/sort/RandomPivot3PartQuickSort.java create mode 100644 src/ru/mail/polis/sort/ShellSort.java diff --git a/src/ru/mail/polis/bench/AverageTimeBench.java b/src/ru/mail/polis/bench/AverageTimeBench.java index 0dc2b7d..5a78fbb 100644 --- a/src/ru/mail/polis/bench/AverageTimeBench.java +++ b/src/ru/mail/polis/bench/AverageTimeBench.java @@ -19,8 +19,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -import ru.mail.polis.sort.BubbleSort; -import ru.mail.polis.sort.Helper; +import ru.mail.polis.sort.*; /** * Created by Nechaev Mikhail @@ -41,10 +40,9 @@ public class AverageTimeBench { @Setup(value = Level.Trial) public void setUpTrial() { - data = new int[10][100]; + data = new int[10][]; for (int i = 0; i < 10; i++) { - //define arrays here - data[i] = Helper.gen(100); + data[i] = Helper.genSortedDESC(10000); } } @@ -59,6 +57,46 @@ public void measureBubbleSort() { BubbleSort.sort(curr); } + @Benchmark + public void measureInsertionSort() { + InsertionSort.sort(curr); + } + + @Benchmark + public void measureImprovedInsertionSort() { + ImprovedInsertionSort.sort(curr); + } + + @Benchmark + public void measureShellSort() { + ShellSort.sort(curr); + } + + @Benchmark + public void measureMergeSort() { + MergeSort.sort(curr); + } + + @Benchmark + public void measureMemoryOptimisedMergeSort() { + MemoryOptimisedMergeSort.sort(curr); + } + + @Benchmark + public void measureQuickSort() { + QuickSort.sort(curr); + } + + @Benchmark + public void measureRandomPivot3PartQuickSort() { + MemoryOptimisedMergeSort.sort(curr); + } + + @Benchmark + public void measureBinaryQuickSort() { + BinaryQuickSort.sort(curr); + } + public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(AverageTimeBench.class.getSimpleName()) diff --git a/src/ru/mail/polis/bench/ImprovedInsertionSortBench.java b/src/ru/mail/polis/bench/ImprovedInsertionSortBench.java new file mode 100644 index 0000000..8c881ab --- /dev/null +++ b/src/ru/mail/polis/bench/ImprovedInsertionSortBench.java @@ -0,0 +1,49 @@ +package ru.mail.polis.bench; + +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import ru.mail.polis.sort.ImprovedInsertionSort; +import ru.mail.polis.sort.Helper; + +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +public class ImprovedInsertionSortBench { + + private int[] a; + + @Setup(value = Level.Invocation) + public void setUpInvocation() { + a = Helper.gen(1000); + } + + @Benchmark + public void measureImprovedInsertionSort(Blackhole bh) { + bh.consume(ImprovedInsertionSort.sort(a)); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(ImprovedInsertionSortBench.class.getSimpleName()) + .warmupIterations(5) + .measurementIterations(5) + .forks(1) + .build(); + + new Runner(opt).run(); + } +} diff --git a/src/ru/mail/polis/bench/InsertionSortBench.java b/src/ru/mail/polis/bench/InsertionSortBench.java new file mode 100644 index 0000000..c711732 --- /dev/null +++ b/src/ru/mail/polis/bench/InsertionSortBench.java @@ -0,0 +1,49 @@ +package ru.mail.polis.bench; + +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import ru.mail.polis.sort.InsertionSort; +import ru.mail.polis.sort.Helper; + +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +public class InsertionSortBench { + + private int[] a; + + @Setup(value = Level.Invocation) + public void setUpInvocation() { + a = Helper.gen(1000); + } + + @Benchmark + public void measureInsertionSort(Blackhole bh) { + bh.consume(InsertionSort.sort(a)); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(InsertionSortBench.class.getSimpleName()) + .warmupIterations(5) + .measurementIterations(5) + .forks(1) + .build(); + + new Runner(opt).run(); + } +} diff --git a/src/ru/mail/polis/sort/BinaryQuickSort.java b/src/ru/mail/polis/sort/BinaryQuickSort.java new file mode 100644 index 0000000..841a162 --- /dev/null +++ b/src/ru/mail/polis/sort/BinaryQuickSort.java @@ -0,0 +1,46 @@ +package ru.mail.polis.sort; + +public class BinaryQuickSort { + + private static final int INT_BINARY_LENGTH = 31; + + public static int[] sort(int[] a) { + if (a == null) + return new int[0]; + + if (a.length == 0 || a.length == 1) + return a; + + performSort(a, 0, a.length - 1, INT_BINARY_LENGTH); + + return a; + } + + public static int[] performSort(int[] arr, int l, int r, int bit){ + if (l < r && !(bit < 0)) { + int index = partition(arr, l, r, bit); + + performSort(arr, l, index, bit - 1); + performSort(arr, index + 1, r, bit - 1); + } + + return arr; + } + + static int getBit(int n, int k) { + return (n >> k) & 1; + } + + public static int partition(int[] a, int l, int r, int bit){ + int i = l; + int j = r; + + while (i <= j) { + while (i < a.length && getBit(a[i], bit) == 0) i++; + while (j > -1 && getBit(a[j], bit) == 1) j--; + if (i <= j) Helper.swap(a, i++, j--); + } + + return j; + } +} \ No newline at end of file diff --git a/src/ru/mail/polis/sort/BubbleSort.java b/src/ru/mail/polis/sort/BubbleSort.java index cb20691..b46bb79 100644 --- a/src/ru/mail/polis/sort/BubbleSort.java +++ b/src/ru/mail/polis/sort/BubbleSort.java @@ -3,6 +3,9 @@ public class BubbleSort { public static int[] sort(int a[]) { + if (a == null) + return new int[0]; + boolean wasSwap = true; int j = 0; while (wasSwap) { diff --git a/src/ru/mail/polis/sort/Helper.java b/src/ru/mail/polis/sort/Helper.java index aeb7a0c..dcfcafb 100644 --- a/src/ru/mail/polis/sort/Helper.java +++ b/src/ru/mail/polis/sort/Helper.java @@ -1,6 +1,7 @@ package ru.mail.polis.sort; import java.util.Random; +import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; public class Helper { @@ -24,4 +25,79 @@ public static int[] gen(int n) { } return a; } + + public static int[] genSortedASC(int n) { + int[] a = Helper.gen(n); + Arrays.sort(a); + return a; + } + + public static int[] genSortedDESC(int n) { + int[] a = Helper.genSortedASC(n); + + for (int i = 0; i < a.length / 2; i++) { + Helper.swap(a, i, a.length - i - 1); + } + + return a; + } + + public static int[] genWorseCaseForMergeSort(int n) { + int[] a = Helper.genSortedASC(n); + Helper.worseCaseForMergeSortSeparate(a); + return a; + } + + public static void worseCaseForMergeSortSeparate(int[] a) { + if(a.length <= 1) + return; + + if(a.length == 2) + { + int swap = a[0]; + a[0] = a[1]; + a[1] = swap; + return; + } + + int i, j; + int m = (a.length + 1) / 2; + int left[] = new int[m]; + int right[] = new int[a.length - m]; + + for(i = 0, j = 0 ;i < a.length; i = i + 2, j++) + left[j]=a[i]; + + for(i = 1, j = 0; i < a.length; i = i + 2, j++) + right[j] = a[i]; + + Helper.worseCaseForMergeSortSeparate(left); + Helper.worseCaseForMergeSortSeparate(right); + Helper.worseCaseForMergeSortMerge(a, left, right); + } + + public static void worseCaseForMergeSortMerge(int[] a, int[] left, int[] right) { + int i, j; + for(i = 0; i < left.length; i++) { + a[i] = left[i]; + } + for(j = 0; j < right.length; j++, i++) { + a[i] = right[j]; + } + } + + public static int binarySearch(int[] a, int key, int right) { + int left = -1; + int mid; + while (left < right - 1) { + mid = left + (right - left) / 2; + + if (a[mid] < key) { + left = mid; + } else { + right = mid; + } + } + return right; + } } diff --git a/src/ru/mail/polis/sort/ImprovedInsertionSort.java b/src/ru/mail/polis/sort/ImprovedInsertionSort.java new file mode 100644 index 0000000..076cf75 --- /dev/null +++ b/src/ru/mail/polis/sort/ImprovedInsertionSort.java @@ -0,0 +1,17 @@ +package ru.mail.polis.sort; + +public class ImprovedInsertionSort { + + public static int[] sort(int a[]) { + if (a == null) + return new int[0]; + + for(int i = 0; i < a.length - 1; i++){ + int p = Helper.binarySearch(a, a[i], i); + int aP = a[i]; + System.arraycopy(a, p, a, p + 1, i - p); + a[p] = aP; + } + return a; + } +} diff --git a/src/ru/mail/polis/sort/InsertionSort.java b/src/ru/mail/polis/sort/InsertionSort.java new file mode 100644 index 0000000..b07d1dc --- /dev/null +++ b/src/ru/mail/polis/sort/InsertionSort.java @@ -0,0 +1,24 @@ +package ru.mail.polis.sort; + +public class InsertionSort { + /*public static void main(String[] args) { + int [] a = {5, 6, 1, 7, 9}; + + InsertionSort.sort(a); + for(int i = 0; i < a.length; i++){ + System.out.print(a[i] + " "); + } + }*/ + + public static int[] sort(int a[]) { + if (a == null) + return new int[0]; + + for(int i = 0; i < a.length; i++){ + for(int j = i; j > 0 && a[j] < a[j - 1]; j--){ + Helper.swap(a, j, j - 1); + } + } + return a; + } +} diff --git a/src/ru/mail/polis/sort/MemoryOptimisedMergeSort.java b/src/ru/mail/polis/sort/MemoryOptimisedMergeSort.java new file mode 100644 index 0000000..98eec5c --- /dev/null +++ b/src/ru/mail/polis/sort/MemoryOptimisedMergeSort.java @@ -0,0 +1,46 @@ +package ru.mail.polis.sort; + + +public class MemoryOptimisedMergeSort { + public static int[] sort(int[] a){ + if (a == null) + return new int[0]; + if (a.length == 0 || a.length == 1) + return a; + + return performSort(a, 0, a.length - 1); + } + + static int[] performSort(int[] a, int min, int max){ + if(max - min == 1){ + if(a[min] > a[max]) { + Helper.swap(a, min, max); + } + } else if(max - min != 0) { + int mid = ( (int) Math.floor(min + (max - min) / 2)); + + performSort(a, min, mid); + performSort(a, mid + 1, max); + mergeArrays(a, min, max, mid); + } + return a; + } + + static void mergeArrays(int[] a, int min, int max, int mid){ + int i = min; + while(i <= mid){ + if(a[i] > a[mid + 1]){ + Helper.swap(a, i, mid + 1); + push(a, mid + 1, max); + } + i++; + } + } + + static void push(int[] aForPush, int s, int e){ + for(int i = s; i < e; i++){ + if(aForPush[i] > aForPush[i + 1]) + Helper.swap(aForPush, i , i + 1); + } + } +} diff --git a/src/ru/mail/polis/sort/MergeSort.java b/src/ru/mail/polis/sort/MergeSort.java new file mode 100644 index 0000000..e13f0a9 --- /dev/null +++ b/src/ru/mail/polis/sort/MergeSort.java @@ -0,0 +1,40 @@ +package ru.mail.polis.sort; + +import java.util.Arrays; + +public class MergeSort { + public static int[] sort(int a[]) { + if (a == null) + return new int[0]; + + if (a.length == 0 || a.length == 1) { + return a; + } + + int avg = a.length / 2; + int[] leftPart = Arrays.copyOf(a, avg); + int[] rightPart = Arrays.copyOfRange(a, avg, a.length); + + leftPart = sort(leftPart); + rightPart = sort(rightPart); + + int i = 0, j = 0; + while (i < leftPart.length && j < rightPart.length) { + if (leftPart[i] <= rightPart[j]) { + a[i + j] = leftPart[i++]; + } else { + a[i + j] = rightPart[j++]; + } + } + + while (i < leftPart.length) { + a[i + j] = leftPart[i++]; + } + + while (j < rightPart.length) { + a[i + j] = rightPart[j++]; + } + + return a; + } +} diff --git a/src/ru/mail/polis/sort/QuickSort.java b/src/ru/mail/polis/sort/QuickSort.java new file mode 100644 index 0000000..a150acb --- /dev/null +++ b/src/ru/mail/polis/sort/QuickSort.java @@ -0,0 +1,43 @@ +package ru.mail.polis.sort; + +public class QuickSort { + + public static int[] sort(int[] a) { + if (a == null) + return new int[0]; + + if (a.length == 0 || a.length == 1) + return a; + + performSort(a, 0, a.length - 1); + + return a; + } + + private static void performSort(int[] a, int low, int high) { + int i = low, j = high; + + int pivot = a[low + (high - low) / 2]; + + while (i <= j) { + while (a[i] < pivot) { + i++; + } + + while (a[j] > pivot) { + j--; + } + + if (i <= j) { + Helper.swap(a, i, j); + i++; + j--; + } + } + //recur + if (low < j) + performSort(a,low, j); + if (i < high) + performSort(a, i, high); + } +} diff --git a/src/ru/mail/polis/sort/RandomPivot3PartQuickSort.java b/src/ru/mail/polis/sort/RandomPivot3PartQuickSort.java new file mode 100644 index 0000000..988134f --- /dev/null +++ b/src/ru/mail/polis/sort/RandomPivot3PartQuickSort.java @@ -0,0 +1,51 @@ +package ru.mail.polis.sort; + +import java.util.Random; + +public class RandomPivot3PartQuickSort { + + public static int[] sort(int[] a) { + if (a == null) + return new int[0]; + + performSort(a, 0, a.length - 1); + return a; + } + + private static void performSort(int a[], int left, int right) { + if (left >= right) { + return; + } + + Random r = new Random(); + + int index = r.nextInt(right - left + 1) + left; + + Helper.swap(a, left, index); + + int x = a[left]; + int j = left; + int k = left; + + for (int i = left + 1; i <= right; i++) { + if (a[i] < x) { + j++; + Helper.swap(a, i , j); + } else if (a[i] == x) { + k++; + j++; + Helper.swap(a, i, j); + Helper.swap(a, k, j); + } + } + + int temp = j; + + for (int i = left; i <= k; i++) { + Helper.swap(a, i, j--); + } + + performSort(a, left, j); + performSort(a, temp + 1, right); + } +} \ No newline at end of file diff --git a/src/ru/mail/polis/sort/ShellSort.java b/src/ru/mail/polis/sort/ShellSort.java new file mode 100644 index 0000000..d6f0d3b --- /dev/null +++ b/src/ru/mail/polis/sort/ShellSort.java @@ -0,0 +1,31 @@ +package ru.mail.polis.sort; + +public class ShellSort { + + public static int[] sort(int a[]) { + if (a == null) + return new int[0]; + + int inner, outer; + int temp; + + int h = 1; + while (h <= a.length / 3) { + h = h * 3 + 1; + } + while (h > 0) { + for (outer = h; outer < a.length; outer++) { + temp = a[outer]; + inner = outer; + + while (inner > h - 1 && a[inner - h] >= temp) { + a[inner] = a[inner - h]; + inner -= h; + } + a[inner] = temp; + } + h = (h - 1) / 3; + } + return a; + } +} diff --git a/tests/ru/mail/polis/sort/valid/Tester.java b/tests/ru/mail/polis/sort/valid/Tester.java index 01d1b2f..96dce62 100644 --- a/tests/ru/mail/polis/sort/valid/Tester.java +++ b/tests/ru/mail/polis/sort/valid/Tester.java @@ -16,6 +16,14 @@ import org.junit.runners.Parameterized; import ru.mail.polis.sort.BubbleSort; +import ru.mail.polis.sort.InsertionSort; +import ru.mail.polis.sort.ImprovedInsertionSort; +import ru.mail.polis.sort.ShellSort; +import ru.mail.polis.sort.MergeSort; +import ru.mail.polis.sort.MemoryOptimisedMergeSort; +import ru.mail.polis.sort.QuickSort; +import ru.mail.polis.sort.RandomPivot3PartQuickSort; +import ru.mail.polis.sort.BinaryQuickSort; import ru.mail.polis.sort.Helper; @RunWith(value = Parameterized.class) @@ -34,6 +42,8 @@ protected void starting(final Description description) { @Parameterized.Parameters(name = "{index}") public static Collection data() { return Arrays.asList(new int[][]{ + null, + {}, {0}, {0, 0, 0, 0}, {4, 3, 2, 1}, @@ -61,4 +71,38 @@ public void test01_checkBubbleSort() throws IOException { Assert.assertTrue(isSorted(BubbleSort.sort(array))); } + @Test + public void test02_checkInsertionSort() throws IOException { + Assert.assertTrue(isSorted(InsertionSort.sort(array))); + } + + @Test + public void test03_checkImprovedInsertionSort() throws IOException { + Assert.assertTrue(isSorted(ImprovedInsertionSort.sort(array))); + } + + @Test + public void test04_checkShellSort() throws IOException { + Assert.assertTrue(isSorted(ShellSort.sort(array))); + } + + @Test + public void test05_checkMergeSort() throws IOException { + Assert.assertTrue(isSorted(MergeSort.sort(array))); + } + + @Test + public void test06_checkMemoryOptimisedMergeSort() throws IOException { + Assert.assertTrue(isSorted(MemoryOptimisedMergeSort.sort(array))); + } + + @Test + public void test07_checkQuickSort() throws IOException { + Assert.assertTrue(isSorted(QuickSort.sort(array))); + } + + @Test + public void test08_checkRandomPivot3PartQuickSort() throws IOException { + Assert.assertTrue(isSorted(RandomPivot3PartQuickSort.sort(array))); + } } From f739c6958d447bf0f074ab150260e2001199c3ad Mon Sep 17 00:00:00 2001 From: mburmistrov Date: Sun, 27 Nov 2016 23:58:24 +0300 Subject: [PATCH 2/2] Add worse qucik case & report --- result.xls | Bin 0 -> 8192 bytes src/ru/mail/polis/bench/AverageTimeBench.java | 6 +++--- src/ru/mail/polis/sort/Helper.java | 14 +++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 result.xls diff --git a/result.xls b/result.xls new file mode 100644 index 0000000000000000000000000000000000000000..8a280477205f56c26b8540fa4a234c66e8e8f371 GIT binary patch literal 8192 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 literal 0 HcmV?d00001 diff --git a/src/ru/mail/polis/bench/AverageTimeBench.java b/src/ru/mail/polis/bench/AverageTimeBench.java index 5a78fbb..ad9e004 100644 --- a/src/ru/mail/polis/bench/AverageTimeBench.java +++ b/src/ru/mail/polis/bench/AverageTimeBench.java @@ -42,7 +42,7 @@ public class AverageTimeBench { public void setUpTrial() { data = new int[10][]; for (int i = 0; i < 10; i++) { - data[i] = Helper.genSortedDESC(10000); + data[i] = Helper.genSortedDESC(1000); } } @@ -52,7 +52,7 @@ public void setUpInvocation() { index = (index + 1) % 10; } - @Benchmark + /*@Benchmark public void measureBubbleSort() { BubbleSort.sort(curr); } @@ -70,7 +70,7 @@ public void measureImprovedInsertionSort() { @Benchmark public void measureShellSort() { ShellSort.sort(curr); - } + }*/ @Benchmark public void measureMergeSort() { diff --git a/src/ru/mail/polis/sort/Helper.java b/src/ru/mail/polis/sort/Helper.java index dcfcafb..5d707e2 100644 --- a/src/ru/mail/polis/sort/Helper.java +++ b/src/ru/mail/polis/sort/Helper.java @@ -42,7 +42,7 @@ public static int[] genSortedDESC(int n) { return a; } - public static int[] genWorseCaseForMergeSort(int n) { + public static int[] genWorstCaseForMergeSort(int n) { int[] a = Helper.genSortedASC(n); Helper.worseCaseForMergeSortSeparate(a); return a; @@ -86,6 +86,18 @@ public static void worseCaseForMergeSortMerge(int[] a, int[] left, int[] right) } } + public static int[] genWorstCaseForQuickSort(int n){ + int[] a = Helper.genSortedASC(n); + worstCaseForQuickSort(a); + return a; + } + + public static void worstCaseForQuickSort(int[] a) + { + for (int i = 2; i < a.length;i++) + swap(a, i, i / 2); + } + public static int binarySearch(int[] a, int key, int right) { int left = -1; int mid;