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
21 changes: 20 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ 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
if (r >= l) {
int mid = l + (r - l) / 2;

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

// If x is smaller, it must be in the left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

// Else, it must be in the right subarray
return binarySearch(arr, mid + 1, r, x);
}

// Element is not present in the array
return -1;
}

// Driver method to test above
Expand All @@ -19,3 +35,6 @@ public static void main(String args[])
System.out.println("Element found at index " + result);
}
}

//Time complexity : O(log n)
//Space complexity : O(log n)
32 changes: 29 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ 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]; // pivot
int i = (low - 1); // index of smaller element

for (int j = low; j < high; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
// Swap the pivot element with the element at i+1
swap(arr, i + 1, high);
return i + 1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
Expand All @@ -22,6 +36,15 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition

if (low < high) {
// Partition index
int pi = partition(arr, low, high);

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

/* A utility function to print array of size n */
Expand All @@ -46,3 +69,6 @@ public static void main(String args[])
printArray(arr);
}
}

//Time complexity : O(n log n)
//Space complexity : O(log n)
21 changes: 18 additions & 3 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ class Node
//Complete this function
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
if (head == null) {
System.out.println("The list is empty.");
return;
}

Node slow_ptr = head;
Node fast_ptr = head;

while (fast_ptr != null && fast_ptr.next != null) {
slow_ptr = slow_ptr.next; // move by 1
fast_ptr = fast_ptr.next.next; // move by 2
}

System.out.println("Middle element: " + slow_ptr.data);
}

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

//Time complexity : O(n)
//Space complexity : O(1)
126 changes: 90 additions & 36 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,96 @@
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
// 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
}

// Main function that sorts arr[l..r] using
// merge()
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;

// Create temp 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;

// Initial index of merged subarray
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[]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy remaining elements of R[]
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
}

/* A utility function to print array of size n */
{
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);
}
}

// Utility function to print array
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
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};

System.out.println("Given Array");
printArray(arr);

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

System.out.println("\nSorted array");
printArray(arr);
}
}
{
int arr[] = {12, 11, 13, 5, 6, 7};

System.out.println("Given Array");
printArray(arr);

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

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

//Time complexity : O(n log n)
//Space complexity : O(n)
115 changes: 79 additions & 36 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,79 @@
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
}

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
}

// A utility function to print contents of arr
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
}

// 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.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}
import java.util.Stack;

class IterativeQuickSort {

// Swapping without using an extra variable (using XOR swap)
void swap(int arr[], int i, int j) {
if (i != j) { // Avoid zeroing elements if i == j
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}

// Partition function (Lomuto partition scheme)
int partition(int arr[], int l, int h) {
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;
}

// Iterative QuickSort using explicit stack
void QuickSort(int arr[], int l, int h) {
// Create stack
Stack<Integer> stack = new Stack<>();

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

// Keep popping until stack is empty
while (!stack.isEmpty()) {
h = stack.pop();
l = stack.pop();

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

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

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

// Utility function to print array
void printArr(int arr[], int n) {
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver code
public static void main(String args[]) {
IterativeQuickSort ob = new IterativeQuickSort();
int arr[] = {4, 3, 5, 2, 1, 3, 2, 3};
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}


//Time complexity : O(n log n)
//Space complexity : O(log n)