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

PreCourse2 complete #1622

Open
wants to merge 1 commit 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
14 changes: 13 additions & 1 deletion Exercise_1.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
/*
Time Complexity: O(lon(n)) because with each iteration in the while loop, the search reduced to half giving it logarithmic TC.
Space Complexity: O(1) It took no extra space.
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
*/
#include <stdio.h>

// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
//Your Code here
while (l<=r) {
int mid = l + (r-l)/2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) r = mid -1;
else l = mid + 1;
}
return -1;
}

int main(void)
Expand Down
34 changes: 25 additions & 9 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <bits/stdc++.h>
/*
Time complexity: O(nlog(n)) - similar to merge sort, logn for division of array and n for partition of sub arrays
Space Complexity: O(1) - no extra space if recursion stack space is ignored.
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
*/
#include <iostream>
using namespace std;

// A utility function to swap two elements
void swap(int* a, int* b)
{
//Your Code here
}


/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
Expand All @@ -15,6 +15,18 @@ of pivot */
int partition (int arr[], int low, int high)
{
//Your Code here
int idx = low-1;
int pivot = arr[high];

for (int j = low; j<high;j++) {
if (arr[j]<=pivot) {
idx++;
swap(arr[j], arr[idx]);
}
}
idx++;
swap(arr[idx], arr[high]);
return idx;
}

/* The main function that implements QuickSort
Expand All @@ -23,7 +35,11 @@ low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
//Your Code here
if (low < high) {
int p_ind = partition(arr, low, high);
quickSort(arr, low, p_ind-1);
quickSort(arr, p_ind+1, high);
}
}

/* Function to print an array */
Expand Down
18 changes: 17 additions & 1 deletion Exercise_3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include<bits/stdc++.h>
/*
Time Complexity: O(n) traverses the list once.
Space Complexity: O(1) It took no extra space.
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
*/

#include<iostream>
using namespace std;

// Struct
Expand All @@ -13,6 +20,15 @@ void printMiddle(struct Node *head)
{
//YourCode here
//Use fast and slow pointer technique
struct Node* slow = head;
struct Node* fast = head;
int mid = 0;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
mid++;
}
cout << slow->data;
}

// Function to add a new node
Expand Down
45 changes: 42 additions & 3 deletions Exercise_4.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
/*
Time Complexity: O(nlog(n)) - Because array division into halves takes log(n) and then merge takes n.
Space Complexity: O(n) - during merge it takes O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
*/

#include<stdlib.h>
#include<stdio.h>

#include <vector>
#include <iostream>

using namespace std;
// 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
vector<int> temp;
int l1 = l;
int l2 = m+1;
while (l1 <= m && l2 <= r) {
if (arr[l1] <= arr[l2]) {
temp.push_back(arr[l1]);
l1++;
}
else {
temp.push_back(arr[l2]);
l2++;
}
}
while (l1<= m) {
temp.push_back(arr[l1]);
l1++;
}
while (l2<= r) {
temp.push_back(arr[l2]);
l2++;
}

for (int i = l; i<=r; i++){
arr[i] = temp[i-l];
}
}

/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
//Your code here
int mid = (r+l)/2;
if (l>=r) return;
mergeSort(arr, l, mid);
mergeSort(arr, mid+1, r);

merge(arr, l, mid, r);
}

/* UTILITY FUNCTIONS */
Expand Down
35 changes: 33 additions & 2 deletions Exercise_5.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include <bits/stdc++.h>
/*
Time complexity: O(nlog(n)) - similar to merge sort, logn for division of array and n for partition of sub arrays
Space Complexity: O(log(n)) - due to stack used for partitioning.
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
*/
#include <iostream>
#include <stack>
using namespace std;

// A utility function to swap two elements
Expand All @@ -10,9 +17,21 @@ void swap(int* a, int* b)
}

/* This function is same in both iterative and recursive*/
int partition(int arr[], int l, int h)
int partition(int arr[], int low, int high)
{
//Do the comparison and swapping here
int idx = low-1;
int pivot = arr[high];

for (int j = low; j<high;j++) {
if (arr[j]<=pivot) {
idx++;
swap(arr[j], arr[idx]);
}
}
idx++;
swap(arr[idx], arr[high]);
return idx;
}

/* A[] --> Array to be sorted,
Expand All @@ -21,6 +40,18 @@ h --> Ending index */
void quickSortIterative(int arr[], int l, int h)
{
//Try to think that how you can use stack here to remove recursion.
stack<pair<int, int>> st;
st.push({l, h});
while (!st.empty()) {
auto curr = st.top();
st.pop();
if(curr.first < curr.second){
int p_ind = partition(arr, curr.first, curr.second);
if (p_ind-1>curr.first) st.push({curr.first, p_ind-1});
if (p_ind+1 <curr.second) st.push({p_ind+1, curr.second});
}
}

}

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