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

PreCourse_2 Completed #1604

Open
wants to merge 5 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
20 changes: 18 additions & 2 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
class BinarySearch {
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

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(x < arr[mid]) {
r = mid-1;
} else if (x > arr[mid]) {
l = mid+1;
} else{
return mid;
}
}
return -1;
}

// Driver method to test above
Expand All @@ -11,7 +27,7 @@ public static void main(String args[])
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
Expand Down
40 changes: 33 additions & 7 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class QuickSort
// Time Complexity : Best Case:- O(nlogn), Worst Case:- O(n²)
// Space Complexity : O(logn)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
Expand All @@ -7,21 +12,42 @@ 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[j];
arr[j] = arr[i];
arr[i] = temp;
}

int partition(int arr[], int low, int high)
int partition(int arr[], int low, int high)
{
//Write code here for Partition and Swap
int leftIndex = low;
int rightIndex = high;
while(leftIndex < rightIndex) {
while(arr[leftIndex] <= arr[high] && leftIndex < rightIndex) {
leftIndex++;
}
while(arr[rightIndex] >= arr[high] && rightIndex > leftIndex) {
rightIndex--;
}
swap(arr, leftIndex, rightIndex);
}
swap(arr, leftIndex, high);
return leftIndex;
}
/* 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
// Recursively sort elements before
// partition and after partition
if(low >= high) {
return;
}
int leftIndex = partition(arr, low, high);
sort(arr, low, leftIndex-1);
sort(arr, leftIndex+1, high);
}

/* A utility function to print array of size n */
Expand All @@ -36,7 +62,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};
int n = arr.length;

QuickSort ob = new QuickSort();
Expand Down
16 changes: 14 additions & 2 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class LinkedList
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class LinkedList
{
Node head; // head of linked list

Expand All @@ -19,7 +24,14 @@ class Node
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
//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("Middle: "+ slow.data);
}

public void push(int new_data)
Expand Down
73 changes: 58 additions & 15 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
class MergeSort
// Time Complexity : O(n log n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

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
}

// 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 merge(int arr[], int l, int m, int r)
{
//Your code here
int leftSize = m - l + 1;
int rightSize = r - m;

int[] left = new int[leftSize];
int[] right = new int[rightSize];

for (int i = 0; i < leftSize; i++)
left[i] = arr[l + i];
for (int j = 0; j < rightSize; j++)
right[j] = arr[m + 1 + j];

int i=0, j=0, k=l;
while(i < leftSize && j < rightSize) {
if(left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
while(i < leftSize) {
arr[k] = left[i];
i++;
k++;
}
while(j < rightSize) {
arr[k] = right[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) {
int m = l + (r - l) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

/* A utility function to print array of size n */
static void printArray(int arr[])
Expand All @@ -33,8 +76,8 @@ public static void main(String args[])
System.out.println("Given Array");
printArray(arr);

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

System.out.println("\nSorted array");
printArray(arr);
Expand Down
51 changes: 46 additions & 5 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
class IterativeQuickSort {
// Time Complexity : Best Case:- O(nlogn), Worst Case:- O(n²)
// Space Complexity : O(logn)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

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

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
//Compare elements and swap.
}
int leftIndex = l;
int rightIndex = h;
while(leftIndex < rightIndex) {
while(arr[leftIndex] <= arr[h] && leftIndex < rightIndex) {
leftIndex++;
}
while(arr[rightIndex] >= arr[h] && rightIndex > leftIndex) {
rightIndex--;
}
swap(arr, leftIndex, rightIndex);
}
swap(arr, leftIndex, h);
return leftIndex;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
//Try using Stack Data Structure to remove recursion.
{
int[] stackArr = new int[h - l + 1];
int top = -1;
stackArr[++top] = l;
stackArr[++top] = h;

while (top >= 0) {
h = stackArr[top--];
l = stackArr[top--];

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

if (p - 1 > l) {
stackArr[++top] = l;
stackArr[++top] = p - 1;
}

if (p + 1 < h) {
stackArr[++top] = p + 1;
stackArr[++top] = h;
}
}
}

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