From d90e26ee5949d0cb0c91f371244225842e028dbe Mon Sep 17 00:00:00 2001 From: NasreenParween Date: Wed, 6 Jul 2022 11:42:17 +0530 Subject: [PATCH] Randomized select --- DAA_Randomized_Select.cpp | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 DAA_Randomized_Select.cpp diff --git a/DAA_Randomized_Select.cpp b/DAA_Randomized_Select.cpp new file mode 100644 index 0000000..cb47f0c --- /dev/null +++ b/DAA_Randomized_Select.cpp @@ -0,0 +1,99 @@ +#include +#include +using namespace std; +template +class random +{ +public: + t a[500]; + int count, n; + void input(); + void display(); + int qsort(t a[], int p, int r); + int partition(t a[], int p, int r); + int randomSelect(t a[], int p, int r, int i); +}; +template +void random::input() +{ + int i; + cout << "Enter the no of elements" << endl; + cin >> n; + for (i = 1;i <= n; i++) + a[i] = rand() % 100; +} +template +void random::display() +{ + for (int i = 1; i <= n;i++) + cout << a[i] << " "; +} +template +int random::partition(t a[], int p, int r) +{ + int x, i, j, temp; + x = a[r]; + i = p - 1; + for (j = p; j <= r - 1; j++) + { + if (a[j] <= x) + { + i = i + 1; + temp = a[j]; + a[j] = a[i]; + a[i] = temp; + } + count++; + } + temp = a[i+1]; + a[i+1] = a[r]; + a[r] = temp; + return i + 1; +} +template +int random::randomSelect(t a[], int p, int r, int i) +{ + int k, q; + if (p == r) + return a[p]; + q = partition(a, p, r); + k = q - p + 1; + if (i == k) + return a[q]; + else if (i < k) + return randomSelect(a, p, r, i); + else + return randomSelect(a, q + 1, r, k); +} +template +int random::qsort(t a[], int p, int r) +{ + int q, i; + if (p < r) + { + q = partition(a, p, r); + qsort(a, p, q - 1); + qsort(a, q + 1, r); + } + return count; +} +int main() +{ + int c; + char ch = 'y'; + while (ch == 'y' || ch == 'Y') + { + random o; + o.input(); + cout << "\n The elements of the array are: "; + o.display(); + o.count = 0; + c = o.qsort(o.a, 1, o.n); + cout << "count: " << c; + cout << "\n After Sorting =]: " << endl; + o.display(); + cout << "\n Do you want to continue?(y/n)"; + cin >> ch; + } + return 0; +}