diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..4a5794a8 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -2,7 +2,17 @@ class BinarySearch { // Returns index of x if it is present in arr[l.. r], else return -1 int binarySearch(int arr[], int l, int r, int x) { - //Write your code here + while(l <= r){ + int mid = l+(r-l)/2; + + if(arr[mid] == x){ + return mid; + }else if(arr[mid] Array to be sorted, @@ -20,6 +33,12 @@ int partition(int arr[], int low, int high) high --> Ending index */ void sort(int arr[], int low, int high) { + if(low= 0){ + //pop high and low + h = stack[top--]; + l = stack[top--]; + + //put pivot at corect place + int p = partition(arr, l, h); + if(p-1 > l){ + stack[++top] = l; + stack[++top] =p-1; + } + if(p + 1 < h){ + stack[++top] = p+1; + stack[++top] = h; + } + } } - + // A utility function to print contents of arr void printArr(int arr[], int n) {