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
16 changes: 15 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
// 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
while (l < r) {
int mid = l + (r-l)/2;
if (arr[mid] == x) {
return mid;
} else if (x > arr[mid]) {
l = mid;
} else {
r = mid;
}
}

return -1;
}

// Driver method to test above
Expand Down
31 changes: 27 additions & 4 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Time Complexity: O(n log n)
// Space Complexity: O(1). It will be O(n) if we are considering the space used by the recursive stack.

class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -7,21 +10,41 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//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 = arr[high];
int i = low - 1;

for(int j=low; j<high; j++) {
if (arr[j] <= pivot) {
i++;

swap(arr, i, j);
}
}

swap(arr, high, i+1);

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
if (low < high) {
int pivotIdx = partition(arr, low, high);

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

/* A utility function to print array of size n */
Expand Down
14 changes: 12 additions & 2 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Time Complexity: O(n) as we have to traverse all elements in the linked list.
// Space Complexity: O(1)

class LinkedList
{
Node head; // head of linked list
Expand All @@ -18,8 +21,15 @@ class Node
//Complete this function
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(slow.data);
}

public void push(int new_data)
Expand Down
45 changes: 39 additions & 6 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
// Time Complexity: O(n log n) as we keep dividing the array into two halves.
// Space Complexity: O(n) as we use a temp array for sorting the elements.

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)
void merge(int arr[], int start, int mid, int right)
{
//Your code here
int i = start, j = mid;

int[] temp = new int[right - start];
int tempIdx = 0;

while (i < mid && j < right) {
if (arr[i] < arr[j]) {
temp[tempIdx++] = arr[i++];
} else {
temp[tempIdx++] = arr[j++];
}
}

while (i < mid) {
temp[tempIdx++] = arr[i++];
}

while (j < right) {
temp[tempIdx++] = arr[j++];
}

for(int k=start; k<right; k++) {
arr[k] = temp[k-start];
}
}

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

int mid = (start + end) / 2;
sort(arr, start, mid);
sort(arr, mid, end);

merge(arr, start, mid, end);
}

/* A utility function to print array of size n */
Expand All @@ -34,7 +67,7 @@ public static void main(String args[])
printArray(arr);

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

System.out.println("\nSorted array");
printArray(arr);
Expand Down
50 changes: 45 additions & 5 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
// Time Complexity: O(n log n)
// Space Complexity: O(n) as we are using auxiliary space in the form of stack

import java.util.*;

class IterativeQuickSort {

void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
int partition(int arr[], int low, int high)
{
//Compare elements and swap.
int pivot = arr[high];
int i = low - 1;

for(int j=low; j<high; j++) {
if (arr[j] <= pivot) {
i++;

swap(arr, i, j);
}
}

swap(arr, high, i+1);

return i+1;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
void QuickSort(int arr[], int low, int high)
{
//Try using Stack Data Structure to remove recursion.
Stack<Integer> stack = new Stack<>();
stack.push(low);
stack.push(high);

while (!stack.isEmpty()) {
high = stack.pop();
low = stack.pop();

int pivotIdx = partition(arr, low, high);

if (pivotIdx - 1 > low) {
stack.push(low);
stack.push(pivotIdx - 1);
}

if (pivotIdx + 1 < high) {
stack.push(pivotIdx + 1);
stack.push(high);
}
}
}

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