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
13 changes: 13 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
// Time complexity : O(logn)
// 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) // element found
return mid;
else if(arr[mid] < x) // element of right half of array
l = mid + 1;
else // element on left half of array
r = mid - 1;
}
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,8 @@
// Time Complexity : O(nlogn)
// worst case : O(n^2)

// Space Complexity : O(logn)
// worst case : O(n)
class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -6,22 +11,40 @@ class QuickSort
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
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
//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,
low --> Starting index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
// Recursively sort elements before
// partition and after partition
if (low < high) {
int partitionIndex = partition(arr, low, high);
sort(arr, low, partitionIndex - 1);
sort(arr, partitionIndex + 1, high);
}
}

/* A utility function to print array of size n */
Expand Down
12 changes: 11 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Time Complexity : O(n/2)
// Space Complexity : O(n)

class LinkedList
{
Node head; // head of linked list
Expand All @@ -19,7 +22,14 @@ class Node
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
//Implement using Fast and slow pointers
Node fastPtr = head;
Node slowPtr = head;
while(fastPtr != null && fastPtr.next != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next.next;
}
System.out.println(slowPtr.data);
}

public void push(int new_data)
Expand Down
44 changes: 42 additions & 2 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
// Time Complexity : O(nlogn)
// Space Complexity : O(n)

import java.util.Arrays;

class MergeSort
{
private int[] tempArr;
// 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
//Your code here
int i = l;
int j = m + 1;
int k = l;

// Merge the temporary arrays
while(i <= m && j <= r) {
if (arr[i] <= arr[j]) {
tempArr[k++] = arr[i++];
}
else {
tempArr[k++] = arr[j++];
}
}

// Copy any remaining elements from left subarray
while (i <= m) {
tempArr[k++] = arr[i++];
}

// Copy any remaining elements from right subarray
while (j <= r) {
tempArr[k++] = arr[j++];
}

for (int p = l; p <= r; p++) {
arr[p] = tempArr[p];
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
//Write your code here
//Call mergeSort from here
if(l < r) {
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 */
Expand All @@ -34,6 +73,7 @@ public static void main(String args[])
printArray(arr);

MergeSort ob = new MergeSort();
ob.tempArr = new int[arr.length];
ob.sort(arr, 0, arr.length-1);

System.out.println("\nSorted array");
Expand Down
40 changes: 39 additions & 1 deletion Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
// 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
//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)
{
//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 partitionIndex = partition(arr, l, h);

if(partitionIndex - 1 > l) {
stack[++top] = l;
stack[++top] = partitionIndex - 1;
}

if(partitionIndex + 1 < h) {
stack[++top] = partitionIndex + 1;
stack[++top] = h;
}
}
}

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