Untitled

 avatar
unknown
plain_text
10 months ago
1.0 kB
18
Indexable
#include <iostream>
#include <random>

using namespace std;

void optimizedRandomizedPivot();

int main() {
    srand(time(NULL));

    for (int i = 0; i < 5; i++) {
        optimizedRandomizedPivot();
    }
}

void optimizedRandomizedPivot() {
    int successfulPivot = 0;
    int items = 1000;

    for (int i = 0; i < 10000; i++) {
        int pivot1 = rand() % items;

        if (pivot1 >= (items * 0.25 + 1) && pivot1 <= (items * 0.75)) {
            successfulPivot++;
        } else {
            int pivot2 = 0;
            if (pivot1 > (items * 0.75 + 1)) {
                pivot2 = (rand() % (pivot1 - 1)) + 1;
            }

            if (pivot1 < (items * 0.25 + 1)) {
                pivot2 = (rand() % (items - (pivot1 + 1) + 1)) + (pivot1);
            }

            if (pivot2 >= (items * 0.25 + 1) && pivot2 <= (items * 0.75)) {
                successfulPivot++;
            }
        }
    }

    cout << "Times that a pivot was successfully chosen within the second quartile: " << successfulPivot << endl;
}



Editor is loading...
Leave a Comment