// QuickSelect algorithm to find the nth largest element in an unsorted array // this version have extra memory usage as it does not mutate the original array int kth_largest(const vector<int> &arr, int k) { if (k < 0 || k >= arr.size()) return -1; srand(time_t()); int pivot = arr[rand() % arr.size()]; // partition. Note that we do *not* add pivot into any array vector<int> L; vector<int> R; for (auto x : arr) { if (x < pivot) { L.push_back(x); } else if (x > pivot) { R.push_back(x); } } // select if (k + 1 <= L.size()) { // it's in L return kth_largest(L, k); } else if (k + 1 > arr.size() - R.size()) { // it's in R return kth_largest(R, k - (arr.size() - R.size())); } else { // pivot itself return pivot; } }
int main(void) { printf("Enter number of elements in max-heap, followed by the max-heap elements, followed by k.\n"); printf("> "); size_t elems; while (scanf("%zu", &elems) == 1) { size_t i; for (i = 0; i < elems; i++) { scanf("%d", &input_heap[i]); } size_t k; scanf("%zu", &k); int res; if (kth_largest(input_heap, elems, k, &res)) { printf("%zu-th largest = %d\n", k, res); } else { printf("Invalid value of k (%zu)\n", k); } printf("> "); } return 0; }
int main() { int n; tree *root = NULL; while (1) { scanf("%d", &n); if (n == -1) break; root = insert_BST_iterative(root, n); } print_awesome(root); printf("\n\n"); int k; scanf("%d", &k); printf("Kth (%d) Largest Element: %d\n", k, kth_largest(root, k)); cleanup(&root); return 0; }
int main(int argc, char const *argv[]) { vector<vector<int>> arrs { {1,5, 6, 1,2, 3, 4, 5}, {2, 4, 1,2, 1,1,1, 4, 6}, {8,7,6,5,4,4,3,2,1,}, {1,2,3,4,5,6,7,8}, {1}, }; vector<int> ks { 3, 4, 5, 3, 0, }; for (int i = 0; i < arrs.size(); ++i) { cout << "Test : " << kth_largest(arrs[i], ks[i]) << "\n"; nth_element(arrs[i].begin(), arrs[i].begin() + ks[i], arrs[i].end()); cout << "Golden: " << arrs[i][ks[i]] << "\n"; } return 0; }