From cb89344125e30d966581b06e894a530929f4c615 Mon Sep 17 00:00:00 2001 From: "ravip1797@gmail.com" Date: Sat, 18 Jan 2025 22:19:50 -0600 Subject: [PATCH] Completed Precourse 2 --- .vscode/c_cpp_properties.json | 18 +++++++++++ .vscode/launch.json | 13 ++++++++ .vscode/settings.json | 59 +++++++++++++++++++++++++++++++++++ Exercise_1.java | 21 ++++++++++++- Exercise_2.java | 24 +++++++++++++- Exercise_3.java | 9 +++++- Exercise_4.java | 52 +++++++++++++++++++++++++++++- Exercise_5.java | 35 +++++++++++++++++++++ 8 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..980fd575 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -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 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..a7cbad5d --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..b9c6ac87 --- /dev/null +++ b/.vscode/settings.json @@ -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 +} \ No newline at end of file diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..a2df04f4 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -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 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