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
17 changes: 16 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ 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; // Find the middle element

// Check if x is present at mid
if (arr[mid] == x)
return mid;

// If x is greater, ignore the left half
if (arr[mid] < x)
l = mid + 1;
// If x is smaller, ignore the right half
else
r = mid - 1;
}
// Element is not present in array
return -1;
}

// Driver method to test above
Expand Down
33 changes: 27 additions & 6 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,43 @@ 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){
//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
// Taking the last element as the pivot
int pivot = arr[high];
int i = (low - 1); // index of the smaller element

for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to the pivot
if (arr[j] <= pivot) {
i++; // increment the index of smaller element
swap(arr, i, j);
}
}
// Swap the pivot element with the element at index (i + 1)
swap(arr, i + 1, high);
return (i + 1); // Return the index of the pivot element
}
/* 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) {
// Find pivot element such that arr[pivot] is in the correct sorted position
int pi = partition(arr, low, high);

// Recursively sort elements before partition and after partition
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}

/* A utility function to print array of size n */
Expand Down
20 changes: 19 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
if (head == null) {
System.out.println("The list is empty.");
return;
}

// Initialize slow and fast pointers
Node slow = head;
Node fast = head;

// Traverse the list
while (fast != null && fast.next != null) {
slow = slow.next; // Move slow pointer by 1
fast = fast.next.next; // Move fast pointer by 2
}

// When the loop ends, slow points to the middle element
System.out.println("Middle element: " + slow.data);
}

public void push(int new_data)
Expand Down Expand Up @@ -50,4 +67,5 @@ public static void main(String [] args)
llist.printMiddle();
}
}
}
}

58 changes: 52 additions & 6 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,63 @@ 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) {
// Find the sizes of the two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;

// Create temporary arrays to hold the data
int L[] = new int[n1];
int R[] = new int[n2];

// Copy data to temporary arrays L[] and R[]
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 temporary arrays back into arr[l..r]
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 any remaining elements of L[] or R[]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {
arr[k] = R[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
if (l < r) {
// Find the middle point
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 */
Expand Down
50 changes: 47 additions & 3 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
import java.util.Stack;

class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
if (i != j) {
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]; // Pivot element is the last element
int i = (l - 1); // Pointer for the smaller element
for (int j = l; j <= h - 1; j++) {
if (arr[j] <= pivot) {
i++; // Increment the index of smaller element
swap(arr, i, j); // Swap arr[i] and arr[j]
}
}
swap(arr, i + 1, h); // Swap the pivot element to its correct position
return (i + 1); // Return the partition index
}

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

// Push the initial values of low and high to the stack
stack.push(l);
stack.push(h);

// While stack is not empty, continue sorting
while (!stack.isEmpty()) {
h = stack.pop(); // Pop the high index
l = stack.pop(); // Pop the low index

// Partition the array and get the pivot index
int p = partition(arr, l, h);

// If there are elements on the left of pivot, push them to stack
if (p - 1 > l) {
stack.push(l);
stack.push(p - 1);
}

// If there are elements on the right of pivot, push them to stack
if (p + 1 < h) {
stack.push(p + 1);
stack.push(h);
}
}
}

// A utility function to print contents of arr
Expand All @@ -33,4 +76,5 @@ public static void main(String args[])
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}
}