Skip to content
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
51 changes: 51 additions & 0 deletions Kth Largest/README.md
Original file line number Diff line number Diff line change
@@ -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.
72 changes: 72 additions & 0 deletions Kth Largest/compare_methods.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

// ---- Forward Declarations ----
int partition(vector<int>& arr, int left, int right);
int quickSelect(vector<int>& arr, int left, int right, int k);
int kthLargest_quickselect(vector<int> arr, int k);

// ---- Sorting method ----
int kthLargest_sort(vector<int> arr, int k) {
sort(arr.begin(), arr.end(), greater<int>());
return arr[k - 1];
}

// ---- nth_element method ----
int kthLargest_nth(vector<int> arr, int k) {
nth_element(arr.begin(), arr.begin() + k - 1, arr.end(), greater<int>());
return arr[k - 1];
}

// ---- Quickselect method ----
int partition(vector<int>& 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<int>& 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<int> arr, int k) {
return quickSelect(arr, 0, arr.size() - 1, k);
}

// ---- Main ----
int main() {
vector<int> 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<milliseconds>(end - start).count() << " ms\n";

start = high_resolution_clock::now();
kthLargest_nth(arr, k);
end = high_resolution_clock::now();
cout << "nth_element: " << duration_cast<milliseconds>(end - start).count() << " ms\n";

start = high_resolution_clock::now();
kthLargest_quickselect(arr, k);
end = high_resolution_clock::now();
cout << "QuickSelect: " << duration_cast<milliseconds>(end - start).count() << " ms\n";

return 0;
}
37 changes: 37 additions & 0 deletions Kth Largest/quickselect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

// Partition function (similar to QuickSort)
int partition(vector<int>& 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<int>& 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<int> arr = {3, 2, 3, 1, 2, 4, 5, 5, 6};
int k = 4;

vector<int> temp = arr;
int result = quickSelect(temp, 0, temp.size() - 1, k);
cout << k << "th largest element is " << result << endl;
return 0;
}
73 changes: 35 additions & 38 deletions SearchingAlgorithms/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
#include<bits/stdc++.h>
#include <bits/stdc++.h>
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<<key<<" found at index "<<mid<<endl;
else
cout<<key<<" not found\n";
cout << key << " not found\n";
}

int main()
{
int n,k,i;
cout<<"Enter the number of elements in the array:- ";
cin>>n;
int arr[n];

cout<<"Enter "<<n<<" elements\n";
for(i=0;i<n;++i)
cin>>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;
}