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
20 changes: 20 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
/*
*
* Time Complexity : O(logn)
* Space Complexity : O(1)
* Did this code successfully run on Leetcode : N/A
* Any problem you faced while coding this : No
*/

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

return -1;
}

// Driver method to test above
Expand Down
41 changes: 40 additions & 1 deletion Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
*
* Time Complexity : O(n^2) ~ Worst case encountered when Array is sorted in descending order
* Space Complexity : O(1)
* Did this code successfully run on Leetcode : Yes
* Any problem you faced while coding this : Struggled to find the base case, weak in recursion
*/


class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -7,12 +16,34 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here
//Your code here
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

int partition(int arr[], int low, int high)
{
//Write code here for Partition and Swap

int pivot = high;
int j = low;
int i = j - 1;

while(j < pivot) {
if(arr[j] < arr[pivot]) {
i++;
swap(arr, i, j);
j++;
} else {
j++;
}
}

i++;
swap(arr, i, j);
return i;

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

if(high <= low) {
return;
}

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
17 changes: 17 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
*
* Time Complexity : O(n)
* Space Complexity : O(1)
* Did this code successfully run on Leetcode : Yes
* Any problem you faced while coding this : No
*/

class LinkedList
{
Node head; // head of linked list
Expand All @@ -20,6 +28,15 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
Node slow = head;
Node fast = head;

while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}

System.out.println("Middle : " + slow.data);
}

public void push(int new_data)
Expand Down
63 changes: 63 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
*
* Time Complexity : O(nlogn) ~ logn to divide, n to merge
* Space Complexity : O(n) ~ temporary arrays initialized during merge
* Did this code successfully run on Leetcode : Yes
* Any problem you faced while coding this :
* -- Struggled to figure out merge code
* -- Found it quite tricky to play with the indices, specially m and r
* -- Referred to this video ~ https://www.youtube.com/watch?v=SHqvb69Qy70

*/


class MergeSort
{
// Merges two subarrays of arr[].
Expand All @@ -6,6 +19,48 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
int n1 = m - l + 1;
int n2 = r - m;

int[] lArr = new int[n1];
int[] rArr = new int[n2];

for(int i = 0;i<n1;i++) {
lArr[i] = arr[l + i];
}

for(int i = 0;i<n2;i++) {
rArr[i] = arr[m + 1 + i];
}

int i = 0;
int j = 0;
int k = l;

while(i < n1 && j < n2) {
if(lArr[i] < rArr[j]) {
arr[k] = lArr[i];
i++;
k++;
} else {
arr[k] = rArr[j];
j++;
k++;
}
}

while(i < n1) {
arr[k] = lArr[i];
i++;
k++;
}

while(j < n2) {
arr[k] = rArr[j];
j++;
k++;
}

}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +69,14 @@ void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if(l == r) {
return;
}

int middle = (l+r)/2;
sort(arr, l, middle);
sort(arr, middle+1, r);
merge(arr, l, middle, r);
}

/* A utility function to print array of size n */
Expand Down
47 changes: 46 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
import java.util.*;

/*
*
* Time Complexity : O(n^2) all are descending order
* Space Complexity : O(n) ~ stack space
* Did this code successfully run on Leetcode : No (Time limit exceeded)
* Any problem you faced while coding this : Swapping without extra variable, if we swap same index then result computes to 0. I didn't know and had to debug
*/

class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
if(i != j) {
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - 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 = h;
int j = l;
int i = j - 1;

while(j < pivot) {
if(arr[j] < arr[pivot]) {
i++;
swap(arr, i, j);
j++;
} else {
j++;
}
}

i++;
swap(arr, i, j);
return i;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
//Try using Stack Data Structure to remove recursion.
Stack<int[]> st = new Stack<>();
st.add(new int[]{l,h});

while(!st.isEmpty()) {
int[] temp = st.pop();
int low = temp[0];
int high = temp[1];
if(high > low) {
int pivot = partition(arr, low, high);
st.add(new int[]{low, pivot-1});
st.add(new int[]{pivot+1, high});
}
}
}

// A utility function to print contents of arr
Expand Down