From e3c2aac3c66e1127c81a18ef4e45d3f7f42e1756 Mon Sep 17 00:00:00 2001 From: Shrinidhi Mane Date: Sat, 10 Jan 2026 23:34:23 -0700 Subject: [PATCH] done --- Exercise_1.java | 11 ++++++++++- Exercise_2.java | 26 +++++++++++++++++++++++++- Exercise_3.java | 13 +++++++++++-- Exercise_4.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ Exercise_5.java | 40 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 129 insertions(+), 5 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..2cd14cea 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -2,7 +2,16 @@ 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 + int mid=(l+r)/2; + if(arr[mid]==x) return mid; + else if(arr[mid]>x) + { + return binarySearch(arr, l, mid, x); + } + + else{ + return binarySearch(arr, mid,r, x);} + //Write your code here } // Driver method to test above diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..7fe4a98b 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -8,10 +8,29 @@ class QuickSort of pivot */ void swap(int arr[],int i,int j){ //Your code here + int temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; } int partition(int arr[], int low, int high) { + int pivot=arr[high]; + int i=low-1; + + for(int j=low;j<=high-1;j++) + { + if(arr[j]<=pivot) + { + i++; + swap(arr, i, j); + } + + + } + + swap(arr, i+1, high); + return i+1; //Write code here for Partition and Swap } /* The main function that implements QuickSort() @@ -20,7 +39,12 @@ int partition(int arr[], int low, int high) high --> Ending index */ void sort(int arr[], int low, int high) { - // Recursively sort elements before + if(low=h) return ; + Stack st =new Stack<>(); + st.push(new int []{l,h}); + + while(!st.isEmpty()) + { + int [] range=st.pop(); + int start=range[0],end=range[1]; + + if(startstart){st.push(new int[]{start, p-1});} + if(p+1