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
22 changes: 20 additions & 2 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@ 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)
{
int high= arr.length;
int low=0;
int mid=0;

//Write your code here
}


while (low<=high) {
mid=low+(high-low)/2; // high-low to avoid integer overflow
if (arr[mid]==x) {
return mid;
}else if (arr[mid]>x) {
high=mid-1;
}else {
low = mid+1;
}
}
return -1;
}
}

public class Exercise_1{
// Driver method to test above
public static void main(String args[])
{
Expand Down
53 changes: 41 additions & 12 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ 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 low,int high){
//Your code here
if (low == high) return;
int tmp = arr[low];
arr[low] = arr[high];
arr[high] = tmp;
}

int partition(int arr[], int low, int high)
{
//Write code here for Partition and Swap
int pivot = arr[high];
int i = low; // place for next <= pivot
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
swap(arr, i, j);
i++;
}
}
swap(arr, i, high);
return i;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -22,27 +36,42 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
if (low >= high) return;
int p = partition(arr, low, high);
sort(arr, low, p - 1);
sort(arr, p + 1, high);

}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}


// Driver program

}

public class Exercise_2{

// Driver program
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;

System.out.println("Before sorted array");
printArray(arr);
System.out.println("---");
QuickSort ob = new QuickSort();
ob.sort(arr, 0, n-1);

System.out.println("sorted array");
printArray(arr);
}
}
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
}
19 changes: 17 additions & 2 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ class Node

/* Function to print middle of linked list */
//Complete this function
// O(n) time, O(1) space
void printMiddle()
{
//Write your code here
if (head == null) return;
//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(" mid=" + slow.data);

}

public void push(int new_data)
Expand All @@ -40,7 +50,12 @@ public void printList()
System.out.println("NULL");
}

public static void main(String [] args)

}

public class Exercise_3{

public static void main(String [] args)
{
LinkedList llist = new LinkedList();
for (int i=15; i>0; --i)
Expand All @@ -50,4 +65,4 @@ public static void main(String [] args)
llist.printMiddle();
}
}
}
}
81 changes: 71 additions & 10 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
// Sizes of the two subarrays
int n1 = m - l + 1;
int n2 = r - m;

// Temporary arrays
int[] L = new int[n1];
int[] R = new int[n2];

// Copy data to temp arrays
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];

for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

// Merge the temp arrays
int i = 0, j = 0;
int k = l;

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

// Copy remaining elements of L[], if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy remaining elements of R[], if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}

}

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

int m = l + (r - l) / 2;

// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);

// Merge the sorted halves
merge(arr, l, m, r);
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}


// Driver method

}

public class Exercise_4{
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
Expand All @@ -39,4 +91,13 @@ public static void main(String args[])
System.out.println("\nSorted array");
printArray(arr);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

}
55 changes: 53 additions & 2 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
import java.util.ArrayDeque;
import java.util.Deque;

class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
if (i == j) return;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

/* 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;
for (int j = l; j < h; j++) {
if (arr[j] <= pivot) {
swap(arr, i, j);
i++;
}
}
swap(arr, i, h);
return i;
}

// Sorts arr[l..h] using iterative QuickSort
// This avoids recursion by simulating the call stack. Average O(n log n), worst O(n²) like normal quicksort.
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
if (arr == null || arr.length <= 1) return;

Deque<int[]> stack = new ArrayDeque<>();
stack.push(new int[]{0, arr.length - 1});

while (!stack.isEmpty()) {
int[] range = stack.pop();
int lo = range[0], hi = range[1];
if (lo >= hi) continue;

int p = partition(arr, lo, hi);

// Optimization: push larger partition first to keep stack smaller
int leftSize = (p - 1) - lo;
int rightSize = hi - (p + 1);

if (leftSize > rightSize) {
stack.push(new int[]{lo, p - 1});
stack.push(new int[]{p + 1, hi});
} else {
stack.push(new int[]{p + 1, hi});
stack.push(new int[]{lo, p - 1});
}
}

}

// A utility function to print contents of arr
Expand All @@ -25,12 +68,20 @@ void printArr(int arr[], int n)
System.out.print(arr[i] + " ");
}

// Driver code to test above

}


public class Exercise_5{

// Driver code to test above
public static void main(String args[])
{
IterativeQuickSort ob = new IterativeQuickSort();
int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 };
ob.printArr(arr, arr.length);
System.out.println("Printed before sorting");
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}
}