Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Prem-Jain authored Mar 24, 2022
1 parent f6c4ceb commit c7e899f
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
95 changes: 95 additions & 0 deletions BinSearch1.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
71 changes: 71 additions & 0 deletions MergeSort.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit c7e899f

Please sign in to comment.