From eeafe61583700c9cc38247634f3c0cb85dceb047 Mon Sep 17 00:00:00 2001 From: Mrunali Vaidya Date: Tue, 28 Apr 2026 19:27:27 +0000 Subject: [PATCH] Complete PreCourse 2 exercises 1 to 5 --- Exercise_1.java | 12 +++++++++- Exercise_2.java | 23 ++++++++++++++++++-- Exercise_3.java | 11 +++++++++- Exercise_4.java | 55 +++++++++++++++++++++++++++++++++++++++++++++- Exercise_5.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 153 insertions(+), 6 deletions(-) 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) {