From c7e899f7c128aebed219cb5fabf2a4dd379b09a7 Mon Sep 17 00:00:00 2001 From: J Prem <88187995+Prem-Jain@users.noreply.github.com> Date: Thu, 24 Mar 2022 11:16:50 +0530 Subject: [PATCH] Add files via upload --- BinSearch1.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ MergeSort.java | 71 ++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 BinSearch1.java create mode 100644 MergeSort.java diff --git a/BinSearch1.java b/BinSearch1.java new file mode 100644 index 0000000..846b8a4 --- /dev/null +++ b/BinSearch1.java @@ -0,0 +1,95 @@ +package Unit_1; + +import java.util.*; + +public class BinSearch1 { + + /*public static int sizeof(int i) { return 4; } + public static int sizeof(long l) { return 8; } + public static int sizeof(int a[]) { return a.length*4;} + public static int sizeof(int a[], int l, int u) { return (u - l + 1)*4;}*/ + + static void BSearchI(int a[], int x) + { + //int space = 0; + int low = 0, high = a.length - 1; + while(low <= high) + { + int mid = (low + high) / 2; + if(x == a[mid]) + { + System.out.println("Element found at index " + mid); + //space = space + sizeof(a) + sizeof(x) + sizeof(mid) + sizeof(high) + sizeof(low); + //System.out.println("Space Complexity = " + space); + return; + } + else if(x < a[mid]) + high = mid - 1; + else + low = mid + 1; + } + System.out.println("Element not found"); + //space = space + sizeof(a) + sizeof(x) + sizeof(mid) + sizeof(high) + sizeof(low); + //System.out.println("Space Complexity = " + space); + return; + } + static void BSearchR(int a[], int low, int high, int x/*, int space*/) + { + //space = space + sizeof(a, low, high) + sizeof(x) + sizeof(high) + sizeof(low); + if(low <= high) + { + + int mid = (low + high) / 2; + //space = space + sizeof(mid); + if(a[mid] == x) + { + System.out.println("Element found at index " + mid); + //System.out.println("Space Complexity = " + space); + return; + } + else if(x < a[mid]) + { + BSearchR(a, low, mid - 1, x/*, space*/); + } + else + { + BSearchR(a, mid + 1, high, x/*, space*/); + } + } + else + { + System.out.println("Element not found"); + //System.out.println("Space Complexity = " + space); + return; + } + } + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter array Size : "); + int n = sc.nextInt(); + int a[] = new int[n]; + System.out.print("Enter array elements : "); + for(int i = 0; i < n; i++) + a[i] = sc.nextInt(); + Arrays.sort(a); + System.out.print("Array is "); + for(int b : a) + { + System.out.print(b + " "); + } + System.out.println(); + System.out.print("Enter the element to be searched : "); + int x = sc.nextInt(); + long start = System.nanoTime(); + BSearchI(a, x); + long end = System.nanoTime(); + System.out.println("Time Complexity for iterative = " + (end - start)*Math.pow(10, -9) + "ns"); + long start1 = System.nanoTime(); + BSearchR(a, 0, n - 1, x/*, 0*/); + long end1 = System.nanoTime(); + System.out.println("Time Complexity for recursive = " + (end1 - start1)*Math.pow(10, -9) + "ns"); + sc.close(); + } +} \ No newline at end of file diff --git a/MergeSort.java b/MergeSort.java new file mode 100644 index 0000000..e8ebc33 --- /dev/null +++ b/MergeSort.java @@ -0,0 +1,71 @@ +package Unit_1; + +import java.util.*; + +public class MergeSort { + + static void print(int a[]) + { + for(int x : a) + System.out.print(x + " "); + System.out.println(); + } + + static void merge(int a[], int low, int mid, int high) + { + int temp[] = new int[high - low + 1]; + int i = low, j = mid + 1, k = 0; + while(i <= mid && j <= high) + { + if(a[i] < a[j]) + temp[k++] = a[i++]; + else + + temp[k++] = a[j++]; + } + while(i <= mid) + { + temp[k++] = a[i++]; + } + while(j <= high) + { + temp[k++] = a[j++]; + } + for(i = low, k = 0; i <= high; i++, k++) + { + a[i] = temp[k]; + } + } + + static void sort(int a[], int low, int high) + { + if(low < high) + { + int mid = (low + high) / 2; + sort(a, low, mid); + sort(a, mid + 1, high); + merge(a, low, mid, high); + print(a); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter array size : "); + int n = sc.nextInt(); + int a[] = new int[n]; + System.out.print("Enter array elements : "); + for(int i = 0; i < n; i++) + a[i] = sc.nextInt(); + System.out.print("Array before Sort : "); + print(a); + long start = System.nanoTime(); + sort(a, 0, n - 1); + long end = System.nanoTime(); + System.out.print("Array afer sort : "); + print(a); + System.out.println("Time COmplexity = " + (end - start)*Math.pow(10, -9) + "Sec"); + sc.close(); + } + +}