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
30 changes: 26 additions & 4 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import java.util.Arrays;

//Time: O(log n) for search.
//Space: 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)
{
int binarySearch(int arr[], int l, int r, int x) {
//Write your code here
//Rules of binary search are 1. array should be sorted, 2. we repeatedly check the middle element

if(arr == null || arr.length == 0)
return -1;

Arrays.sort(arr);

while(l <= r) {
int mid = l+ (r -l) /2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < x) {
l = mid + 1;
} else {
r = mid -1;
}
}

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 arr[] = { 5, 3, 19, 30, 1 };
int n = arr.length;
int x = 10;
int x = 3;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
Expand Down
29 changes: 28 additions & 1 deletion Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//TimeComplexity = O(nlogn), worst case O(n^2)
//SpaceComplexity = O(logn)
class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -7,12 +9,30 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here

if (i == j) return;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;

}

int partition(int arr[], int low, int high)
{
//Write code here for Partition and Swap
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;

}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -22,6 +42,13 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition

if (low < high) {
int pivot = partition(arr, low, high);

sort(arr, low, pivot - 1);
sort(arr, pivot + 1, high);
}
}

/* A utility function to print array of size n */
Expand Down
13 changes: 13 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
if (head == null) {
return;
}

Node slow = head;
Node fast = head;

while(fast!= null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
System.out.println(slow.data);

}

public void push(int new_data)
Expand Down
126 changes: 85 additions & 41 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,86 @@
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)
import java.util.Arrays;

//Time Complexity = O(nlogn)
//Space Complexity = O(n)

class MergeSort
{
//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);
}
}
// 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[] left = Arrays.copyOfRange(arr, l, m+1);
int[] right = Arrays.copyOfRange(arr, m+ 1, r+1);
int i = 0;
int j = 0;
int k = l;

while(i < left.length && j < right.length) {
if (left[i] <= right[j]) {
arr[k] = left[i];

i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}

while (i < left.length) {
arr[k] = left[i];
i++;
k++;
}
while (j < right.length) {
arr[k] = right[j];
j++;
k++;
}

}

// 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
if (l >= r) {
return;
}

int mid = l + (r - l) / 2;

sort(arr, l, mid);
sort(arr, mid+1, r);
merge(arr, l, mid, r);

}

/* 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);
}
}
48 changes: 48 additions & 0 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
//Time Complexity = O(nlogn) Worst case O(n^2)
//Space Complexity = O(logn) Worst case O(n)
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable

if (i == j) return;
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];

}

/* 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 -1; 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.
if (l >= h) return;

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 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
Expand Down