Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done Precourse-2 #1631

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
24 changes: 20 additions & 4 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
class BinarySearch {
// Time complexity O(1) best case average or worst case O(logN).
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;

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[])
{
BinarySearch ob = new BinarySearch();
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
int x = 2;
int result = ob.binarySearch(arr, 0, n - 1, x);

if (result == -1)
System.out.println("Element not present");
else
Expand Down
40 changes: 35 additions & 5 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,51 @@ 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;

for (int j = low; j < high; j++) {

if (arr[j] < pivot) {
swap(arr, i, j);
i++;
}
}

// Swap the pivot element with the element at index i
swap(arr, i, high);

// Return the pivot index
return i;
}

/* 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
// partition and after partition

if (low < high) {
// Partition the array
int pivot = partition(arr, low, high);

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

/* A utility function to print array of size n */
Expand All @@ -36,7 +66,7 @@ static void printArray(int arr[])
// Driver program
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int arr[] = {10, 7, 8, 9, 1, 5, 2};
int n = arr.length;

QuickSort ob = new QuickSort();
Expand Down
24 changes: 20 additions & 4 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Node
Node next;
Node(int d)
{
data = d;
next = null;
this.data = d;
this.next = null;
}
}

Expand All @@ -19,8 +19,24 @@ class Node
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
}
//Implement using Fast and slow pointers

if(head == null) {
System.out.println("list is empty");

return;
}

Node slow = head;
Node fast = head;

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}

System.out.println("Middle element is --> " + slow.data);
}

public void push(int new_data)
{
Expand Down
122 changes: 88 additions & 34 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,96 @@
class MergeSort
{
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) {
//Your code here

// Sizes of the two subarrays
int n1 = m - l + 1;
int n2 = r - m;

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

// Copy data to temporary 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];
}

int i = 0, j = 0, 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
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
}

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;

// Recursively 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();
}

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 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);
}
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);
}
}
56 changes: 50 additions & 6 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
class IterativeQuickSort {
import java.util.Stack;

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

/* 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]; // Choosing the last element as pivot
int i = l - 1; // Index of smaller element

for (int j = l; j < h; 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, h);
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.

Stack<int[]> stack = new Stack<>();

// Push initial values of l and h to the stack
stack.push(new int[] { l, h });

while (!stack.isEmpty()) {
// Pop h and l
int[] range = stack.pop();
l = range[0];
h = range[1];

// Set pivot element at its correct position
int p = partition(arr, l, h);

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

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

// A utility function to print contents of arr
Expand All @@ -29,8 +72,9 @@ void printArr(int arr[], int n)
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);
// int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 };
int arr[] = { 5, 4, 3, 2, 1 };
ob.QuickSort(arr, 0, arr.length - 1);
ob.printArr(arr, arr.length);
}
}