From 816236b477eaba69746ef63110eaf506dd81eb74 Mon Sep 17 00:00:00 2001 From: omuphade77 Date: Mon, 27 Oct 2025 16:28:58 +0530 Subject: [PATCH] Added new files quickselect.cpp readme.md and compare_method.cpp also fix binary search --- Kth Largest/README.md | 51 +++++++++++++++++++ Kth Largest/compare_methods.cpp | 72 ++++++++++++++++++++++++++ Kth Largest/quickselect.cpp | 37 ++++++++++++++ SearchingAlgorithms/binary_search.cpp | 73 +++++++++++++-------------- 4 files changed, 195 insertions(+), 38 deletions(-) create mode 100644 Kth Largest/README.md create mode 100644 Kth Largest/compare_methods.cpp create mode 100644 Kth Largest/quickselect.cpp diff --git a/Kth Largest/README.md b/Kth Largest/README.md new file mode 100644 index 00000000..65de74d4 --- /dev/null +++ b/Kth Largest/README.md @@ -0,0 +1,51 @@ +# Kth Largest Element + +This folder contains multiple C++ methods to find the Kth largest element in an array. + +## Implementations +| File | Method | Time Complexity | Space Complexity | +|------|---------|------------------|------------------| +| `maxheap.cpp` | Max Heap | O(n + k log n) | O(n) | +| `minheap.cpp` | Min Heap | O(n log k) | O(k) | +| `nth_element.cpp` | STL nth_element | O(n) average | O(1) | +| `quickselect.cpp` | QuickSelect Algorithm | O(n) average, O(n²) worst | O(1) | + +## Example Input/Output +**Input:** +arr = [3,2,1,5,6,4], k = 2 +**Output:** +5 + +## Benchmark +Use `compare_methods.cpp` to compare performance of all methods. + +Run: +```bash +g++ compare_methods.cpp -o compare && ./compare +Next Ideas + +Add test cases for correctness. + +Visualize runtime using Python matplotlib. + +Add median-of-medians pivot optimization in QuickSelect. + +Create a header file (kth_largest.h) to reuse methods easily. + +--- + +## 💡 Step 4: What You Can Add Next + +| Category | Ideas | +|-----------|--------| +| ⚙️ **Algorithms** | Add Median-of-Medians (guaranteed O(n) Quickselect), or Randomized Quickselect | +| 📊 **Analysis** | Add time/space complexity table in markdown | +| 🧪 **Testing** | Add `test_kth_largest.cpp` to automatically test all methods | +| 🧰 **Reusability** | Create a header file `kth_largest.h` for reusable function declarations | +| 📈 **Visualization** | Add a Python file `visualize_performance.py` that plots execution time of each method for varying `n` | +| 🧾 **Docs** | Add a small “CONTRIBUTING.md” explaining how others can add new algorithms (open-source style) | + +--- + +Would you like me to make you the **test file (`test_kth_largest.cpp`)** next — to check correctness of all your methods automatically? +That will make your folder look *complete and professional*, like a real open-source module. diff --git a/Kth Largest/compare_methods.cpp b/Kth Largest/compare_methods.cpp new file mode 100644 index 00000000..7d20250a --- /dev/null +++ b/Kth Largest/compare_methods.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; +using namespace chrono; + +// ---- Forward Declarations ---- +int partition(vector& arr, int left, int right); +int quickSelect(vector& arr, int left, int right, int k); +int kthLargest_quickselect(vector arr, int k); + +// ---- Sorting method ---- +int kthLargest_sort(vector arr, int k) { + sort(arr.begin(), arr.end(), greater()); + return arr[k - 1]; +} + +// ---- nth_element method ---- +int kthLargest_nth(vector arr, int k) { + nth_element(arr.begin(), arr.begin() + k - 1, arr.end(), greater()); + return arr[k - 1]; +} + +// ---- Quickselect method ---- +int partition(vector& arr, int left, int right) { + int pivot = arr[right]; + int i = left; + for (int j = left; j < right; j++) { + if (arr[j] > pivot) { + swap(arr[i], arr[j]); + i++; + } + } + swap(arr[i], arr[right]); + return i; +} + +int quickSelect(vector& arr, int left, int right, int k) { + if (left <= right) { + int pi = partition(arr, left, right); + if (pi == k - 1) return arr[pi]; + else if (pi > k - 1) return quickSelect(arr, left, pi - 1, k); + else return quickSelect(arr, pi + 1, right, k); + } + return -1; +} + +int kthLargest_quickselect(vector arr, int k) { + return quickSelect(arr, 0, arr.size() - 1, k); +} + +// ---- Main ---- +int main() { + vector arr(1e5); + generate(arr.begin(), arr.end(), rand); + int k = 50; + + auto start = high_resolution_clock::now(); + kthLargest_sort(arr, k); + auto end = high_resolution_clock::now(); + cout << "Sorting Method: " << duration_cast(end - start).count() << " ms\n"; + + start = high_resolution_clock::now(); + kthLargest_nth(arr, k); + end = high_resolution_clock::now(); + cout << "nth_element: " << duration_cast(end - start).count() << " ms\n"; + + start = high_resolution_clock::now(); + kthLargest_quickselect(arr, k); + end = high_resolution_clock::now(); + cout << "QuickSelect: " << duration_cast(end - start).count() << " ms\n"; + + return 0; +} diff --git a/Kth Largest/quickselect.cpp b/Kth Largest/quickselect.cpp new file mode 100644 index 00000000..3687a00a --- /dev/null +++ b/Kth Largest/quickselect.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +// Partition function (similar to QuickSort) +int partition(vector& arr, int left, int right) { + int pivot = arr[right]; + int i = left; + for (int j = left; j < right; j++) { + if (arr[j] > pivot) { // for Kth largest (descending) + swap(arr[i], arr[j]); + i++; + } + } + swap(arr[i], arr[right]); + return i; +} + +// Quickselect recursive function +int quickSelect(vector& arr, int left, int right, int k) { + if (left <= right) { + int pi = partition(arr, left, right); + if (pi == k - 1) return arr[pi]; + else if (pi > k - 1) return quickSelect(arr, left, pi - 1, k); + else return quickSelect(arr, pi + 1, right, k); + } + return -1; +} + +int main() { + vector arr = {3, 2, 3, 1, 2, 4, 5, 5, 6}; + int k = 4; + + vector temp = arr; + int result = quickSelect(temp, 0, temp.size() - 1, k); + cout << k << "th largest element is " << result << endl; + return 0; +} diff --git a/SearchingAlgorithms/binary_search.cpp b/SearchingAlgorithms/binary_search.cpp index 7a9b65c1..3c07a1d6 100644 --- a/SearchingAlgorithms/binary_search.cpp +++ b/SearchingAlgorithms/binary_search.cpp @@ -1,49 +1,46 @@ -#include +#include using namespace std; -void bsearch(int a[],int n,int key) +void bsearch(int a[], int n, int key) { - sort(a,a+n); - int start,end,mid; - - start = 0; - end = n-1; - bool found = false; - while(start<=end) - { - mid = (start+end)/2; - if(a[mid]==key) + sort(a, a + n); // remove if input is already sorted + int start = 0, last = n - 1, mid; + bool found = false; + + while (start <= last) { - found = true; - break; + mid = (start + last) / 2; + if (a[mid] == key) + { + found = true; + break; + } + else if (a[mid] > key) + last = mid - 1; + else + start = mid + 1; } + + if (found) + cout << key << " found at index " << mid << endl; else - if(a[mid]>key) - end = mid-1; - else - start = mid+1; - } - if(found) - cout<>n; - int arr[n]; - - cout<<"Enter "<>arr[i]; - - cout<<"Enter the element to be searched:- "; - cin>>k; - - bsearch(arr,n,k); - return 0; -} + int n, k; + cout << "Enter the number of elements in the array:- "; + cin >> n; + int arr[n]; + cout << "Enter " << n << " elements\n"; + for (int i = 0; i < n; ++i) + cin >> arr[i]; + + cout << "Enter the element to be searched:- "; + cin >> k; + + bsearch(arr, n, k); + return 0; +}