Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions Exercise_1.java → BInarySearch.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
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
}

// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
//Time Complexity : O(log n)
//Space Complexity : O(1)
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
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] > x) {
return binarySearch(arr, l, mid - 1, x);
}
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}

// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
36 changes: 0 additions & 36 deletions Exercise_5.java

This file was deleted.

73 changes: 73 additions & 0 deletions IterativeQuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Time Complexity : O(n log n)
//Space Complexity : O(log n) in worst case and O(1) in best case
class IterativeQuickSort
{
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
Comment on lines +5 to +11

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
int pivot = arr[h];
int i = l - 1;
for (int j = l; j < h; j++) {
if (arr[j] < pivot)
{
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, h);
return i + 1;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
int [] stack = new int[h - l + 1];
int top = -1;
stack[++top] = l;
stack[++top] = h;
while (top >= 0)
{
h = stack[top--];
l = stack[top--];
int pi = partition(arr, l, h);
if (pi - 1 > l)
{
stack[++top] = l;
stack[++top] = pi - 1;
}
if (pi + 1 < h)
{
stack[++top] = pi + 1;
stack[++top] = h;
}
}
}

// A utility function to print contents of arr
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
}

// Driver code to test above
public static void main(String args[])
{
IterativeQuickSort ob = new IterativeQuickSort();
int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 };
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}
118 changes: 77 additions & 41 deletions Exercise_4.java → MergeSort.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
//Your code here
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};

System.out.println("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);

System.out.println("\nSorted array");
printArray(arr);
}
//Time Complexity : O(n log n)
//Space Complexity : O(log n) in worst case and O(1) in best case
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n1 = m - l + 1;
int n2 = r-m;
int L[] = new int [n1];
int R[] = new int [n2];
for(int i=0;i<n1;i++)
{
L[i] = arr[l+i];
}
for(int i=0;i<n2;i++)
{
R[i] = arr[m+1+i];
}
int i=0, j=0, k=l;
while(i<n1 && j<n2)
{
if(L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
if(l < r)
{
int m = l + (r-l)/2;
sort(arr, l, m);
sort(arr, m+1, r);
merge(arr, l, m, r);
}
//Call mergeSort from here
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};

System.out.println("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);

System.out.println("\nSorted array");
printArray(arr);
}
}
Loading