diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..50ddc5f9 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -3,6 +3,19 @@ class BinarySearch { int binarySearch(int arr[], int l, int r, int x) { //Write your code here + while(l<=r){ + int mid = l + (r-l)/2; // getting the mid point + if(arr[mid]==x){ + return mid; + } + else if(arr[mid] Array to be sorted, @@ -22,6 +37,11 @@ void sort(int arr[], int low, int high) { // Recursively sort elements before // partition and after partition + if(low s = new Stack<>(); + s.push(l); + s.push(h); // stack is used to mimic recursive call + + while(!s.isEmpty()){ + h=s.pop(); + l=s.pop(); + int pivot = partition(arr, l, h); // getting pivot and doing swapping + + if(lpivot+1){ + s.push(h); + s.push(pivot+1); + } + } } // A utility function to print contents of arr