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
18 changes: 18 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ class BinarySearch {
int binarySearch(int arr[], int l, int r, int x)
{
//Write your code here
while (l <= r){

int m = (l + r) / 2;

if (arr[m] == x) {
return m;

}
else if (arr[m] > x) {
r = m - 1;

}
else {
l = m + 1;
}
}

return -1;
}

// Driver method to test above
Expand Down
24 changes: 22 additions & 2 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@ class QuickSort
pivot and all greater elements to right
of pivot */
void swap(int arr[],int i,int j){
//Your code here
//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); // 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(arr, i + 1, high);
return i + 1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
Expand All @@ -22,6 +37,11 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}

/* A utility function to print array of size n */
Expand Down
15 changes: 15 additions & 0 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ 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)
{
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}

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

public void push(int new_data)
Expand Down
53 changes: 53 additions & 0 deletions Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@ class MergeSort
void merge(int arr[], int l, int m, int r)
{
//Your code here
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;
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
Expand All @@ -14,6 +56,17 @@ void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if (l < r) {
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
44 changes: 44 additions & 0 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
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];
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<Integer> stack = new Stack<>();

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

// Keep popping from stack while it's not empty
while (!stack.isEmpty()) {
h = stack.pop();
l = stack.pop();

int p = partition(arr, l, h);

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

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

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