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

Completed Precourse 2 #1640

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
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "macos-clang-arm64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [
""
]
}
],
"version": 4
}
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "/Users/rav_1797/PreCourse-2",
"program": "/Users/rav_1797/PreCourse-2/build/Debug/outDebug"
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "clang",
"C_Cpp_Runner.cppCompilerPath": "clang++",
"C_Cpp_Runner.debuggerPath": "lldb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
21 changes: 20 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
//Overall Time Complexity is O(logn).
//Overall Space Complexity is 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; // Find the mid
if(arr[mid] == x){ // Check if mid is the element that we are searching
return mid; // If yes return mid.
}
else{
if(x<arr[mid]){ // Check if target is less than the mid element
r = mid -1; // Then move the right pointer to mid-1 position
}
else{
l = mid+1;// Else move the left pointer to mid+1 position
}
}
}
return -1;

}


// Driver method to test above
public static void main(String args[])
Expand Down
24 changes: 23 additions & 1 deletion Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//Overall Time Complexity is O(n^2)
//Overall Space Complexity is O(logn)
class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -7,11 +9,26 @@ 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)
{
int pt_index = arr[high];
int i = low - 1;
for(int j = low ; j<high; j++){
if(arr[j]<=pt_index){
i+=1;
swap(arr,i,j);

}
}
swap(arr, i + 1, high);
return i + 1;

//Write code here for Partition and Swap
}
/* The main function that implements QuickSort()
Expand All @@ -22,6 +39,11 @@ void sort(int arr[], int low, int high)
{
// Recursively sort elements before
// partition and after partition
if (low < high){
int pivot = partition(arr, low, high);
sort(arr,low,pivot-1);
sort(arr, pivot+1, high);
}
}

/* A utility function to print array of size n */
Expand Down
9 changes: 8 additions & 1 deletion Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ class Node
/* Function to print middle of linked list */
//Complete this function
void printMiddle()
{
{
Node fast = head;
Node slow = head;
while (fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
System.out.println(slow.data);
//Write your code here
//Implement using Fast and slow pointers
}
Expand Down
52 changes: 51 additions & 1 deletion Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
//Overall Time Complexity is O(nlogn)
//Overall Space Complexity is O(n)


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
//Your code here
int n1 = m - l + 1; // left array size
int n2 = r - m; // right array size

int leftArray[] = new int[n1];
int rightArray[] = new int[n2];

for(int i = 0; i<n1; i++){ //Copy the data into leftvsubarray and right subarray
leftArray[i] = arr[l+i];
}
for(int j = 0; j<n2; j++){
rightArray[j] = arr[m+1+j];
}
int i = 0, j = 0, k = l;
while(i<n1 && j<n2){
if(leftArray[i]<=rightArray[j]){ //Compare and merge the values into the main array
arr[k] = leftArray[i];
i++;
}
else{
arr[k] = rightArray[j];
j++;
}
k++;
}

while(i<n1){
arr[k] = leftArray[i];
i++;
k++;
}
while(j<n2){
arr[k] = rightArray[j];
j++;
k++;
}


}

// Main function that sorts arr[l..r] using
Expand All @@ -14,6 +55,15 @@ void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if(l<r){
int mid = l + (r-l)/2;

sort(arr,l,mid); // Split into 2 arrays
sort(arr, mid + 1, r);

merge(arr,l, mid, r);

}
}

/* A utility function to print array of size n */
Expand Down
35 changes: 35 additions & 0 deletions Exercise_5.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
//Overall Time Complexity is O(n^2)
//Overall Space Complexity is O(n)
import java.util.*;
class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
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 pt_index = arr[h];
int i = l - 1;
for(int j = l ; j<h; j++){
if(arr[j]<=pt_index){
i+=1;
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<int[]> stack = new Stack<>();

stack.push(new int[]{l, h});
while (!stack.isEmpty()) {
int[] bounds = stack.pop();
int start = bounds[0];
int end = bounds[1];

int pivotIndex = partition(arr, start, end);

if (pivotIndex - 1 > start) {
stack.push(new int[]{start, pivotIndex - 1});
}

if (pivotIndex + 1 < end) {
stack.push(new int[]{pivotIndex + 1, end});
}
}
}

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