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
31 changes: 27 additions & 4 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
class BinarySearch {
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
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 the leftmost point of the array is less than or equal to the rightmost
while (l <= r) {

// Finds middle point of array
int mid = l + (r - l) / 2;

//If the midpoint is the target, return it
if (arr[mid] == x) {
return mid;
} else {
// If the target is greater than the midpoint, update the left index to be one ahead of the midpoint
if (arr[mid] < x) {
l = mid + 1;
// If the target is less than the midpoint, update the right index to be one less the midpoint
} else if (arr[mid] > x) {
r = mid - 1;
}
}
}
return -1;
}

// Driver method to test above
public static void main(String args[])
Expand Down
38 changes: 28 additions & 10 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
class QuickSort
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Unlike Binary Search, there was a lot I had to lookup for this one

class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here

void swap(int arr[], int i, int j){
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 partition(int arr[], int low, int high) {
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,
low --> Starting index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
void sort(int arr[], int low, int high) {
if (high > low) {
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
18 changes: 17 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class LinkedList
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Had to lookup fast and slow pointers
class LinkedList
{
Node head; // head of linked list

Expand All @@ -16,10 +20,22 @@ class Node

/* Function to print middle of linked list */
//Complete this function

//
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
if (head == null) {
System.out.println("Null Linked List");
}
Node fast = head;
Node slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
System.out.println(slow.data);
}

public void push(int new_data)
Expand Down
68 changes: 59 additions & 9 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,69 @@
class MergeSort
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None
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
}
void merge(int arr[], int l, int m, int r) {
int size1 = m - l + 1;
int size2 = r - m;

int left[] = new int[size1];
int right[] = new int[size2];

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

for (int j = 0; j < size2; ++j) {
right[j] = arr[m + 1 + j];
}

// Merge the temp arrays
// Initial indexes of first and second subarrays
int i = 0, j = 0;

int k = l;
while (i < size1 && j < size2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
}
else {
arr[k] = right[j];
j++;
}
k++;
}

while (i < size1) {
arr[k] = left[i];
i++;
k++;
}

while (j < size2) {
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
void sort(int arr[], int l, int r) {
if (l < r) {

int mid = (l + r) / 2;

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

merge(arr, l, mid, r);
}
}

/* A utility function to print array of size n */
Expand Down
59 changes: 50 additions & 9 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
import java.util.Stack;

// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Unable to get swap to work without tmp. See commented code for attempt
class IterativeQuickSort {
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

// 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 = 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.
void QuickSort(int arr[], int l, int h) {
Stack<Integer> s = new Stack<>();
s.push(0);
s.push(arr.length - 1);

while (!s.isEmpty()) {
int end = s.pop();
int start = s.pop();

if (start >= end) {
continue;
}

int pivot = partition(arr, start, end);
if (pivot + 1 < end) {
s.push(pivot + 1);
s.push(end);
}

if (start < pivot - 1) {
s.push(start);
s.push(pivot - 1);
}
}
}

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