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
26 changes: 24 additions & 2 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
class BinarySearch {
// Returns index of x if it is present in arr[l.. r], else return -1
// Returns index of x if it is present in arr[l.. r], else return -1
//time - O(log n)
// space - O(1)
int binarySearch(int arr[], int l, int r, int x)
{
//Write your code here
}
if (r == 0) // if size of array is zero then return -1
return -1;
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[])
Expand Down
29 changes: 24 additions & 5 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,41 @@ class QuickSort
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
// time - O(nlog(n))
// space - O(n)
void swap(int arr[],int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//Your code here
}

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; 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(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
11 changes: 10 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class LinkedList
{
{
//time - O(n)
Node head; // head of linked list

/* Linked list node */
Expand All @@ -20,6 +21,14 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
Node fast = head;
Node slow = 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
47 changes: 43 additions & 4 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,57 @@ class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
// Second subarray is arr[m+1..r]
// Time - O(n log n)
// space - O(n)
void merge(int arr[], int l, int m, int r)
{
//Your code here
}
//Your code here
int n1 = m - l + 1; // size of first array
int n2 = r - m;
int [] subArray1 = new int[n1];
int [] subarray2 = new int[n2];
for(int i=0;i<n1;i++)
subArray1[i] = arr[l+i];
for (int j=0;j<n2;j++)
subarray2[j] = arr[m+1+j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (subArray1[i] <= subarray2[j]) {
arr[k] = subArray1[i];
i++;
} else {
arr[k] = subarray2[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = subArray1[i];
i++;
k++;
}
while (j < n2) {
arr[k] = subarray2[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
//Call mergeSort from here
if(l < r){
int m = l+(r-l)/2; // middle point
sort(arr, l, m); // sort the first half
sort(arr, m+1, r); // sort the second half
merge(arr,l,m,r); // merge the sorted half
}
}

/* A utility function to print array of size n */
Expand Down
39 changes: 35 additions & 4 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
class IterativeQuickSort {
import java.util.Stack;
//time - O(n log n)
// space - O(log n )
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
//Try swapping without extra variable
if (i == j) return;
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; 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.
Stack<int[]> stack = new Stack<>();
stack.push(new int[]{l,h});

while (!stack.isEmpty()){
int[] temp = stack.pop();
l = temp[0];
h = temp[1];
if(l < h){
int pi = partition(arr, l, h);
stack.push(new int[]{l,pi-1});
stack.push(new int[]{pi+1, h});
}
}

}

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