Question

(C++)Randomized Quicksort: Write C++ codes for randomized quicksort. You may need rand() to generate random numbers....

(C++)Randomized Quicksort: Write C++ codes for randomized quicksort. You may need rand()

to generate random numbers. Run the randomized quicksort 5 times for input array A = {1, 2, 3, …, 99,

100} and report the 5 running times.

Homework Answers

Answer #1
#include <bits/stdc++.h>

using namespace std;

void swap(int array[], int i, int j)
{
        //This function swaps i and j indexes of array
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
}

void randomize_array(int array[]) {
        //This function randomizes the given array positions
        srand(time(NULL));
        for (int i = 99; i > 0; i--) {
                int j = rand() % (i + 1);
                swap(array, i, j);
        }
}

int Partition(int array[], int start, int end) {
        int i, pivot = end, j = start;
        for (i = start; i < end; i++) {
                if (array[i] < array[pivot]) {
                        swap(array, i, j);
                        j++;
                }
        }
        swap(array, pivot, j);
        return j;
}
int RandomPartition(int array[], int start, int end) {
        // Random selection of pivot.
        int pivot;
        srand(time(NULL));
        pivot = start + rand() % (end - start + 1); // Randomizing the pivot value from sub-array.
        swap(array, end, pivot);
        return Partition(array, start, end);
}
void quickSort(int array[], int start, int end) {
        //Algorithm for quickSort
        if (start < end) {
                int pivot = RandomPartition(array, start, end); //randomly choose pivot
                quickSort(array, start, pivot - 1);
                quickSort(array, pivot + 1, end);
        }
}
int main() {
        int array[100];
        int no_of_times = 5;

        clock_t start_time, end_time;

        while (no_of_times--) {
                //Iterate 5 times
                for (int i = 1; i <= 100; i++)
                        array[i - 1] = i;
                //Measure start time of algorithm
                start_time = clock();
                randomize_array(array);
                //Sort the elements using quick sort algorithm
                quickSort(array, 0, 99);
                //Measure end time of algorithm
                end_time = clock();
                cout << (5 - no_of_times) << ")Array after sorting is \n";
                for (int i = 0; i < 100; i++)
                        cout << array[i] << " ";
                cout << endl;
                double time_elapsed = double(end_time - start_time) / double(CLOCKS_PER_SEC);
                cout << "\nDuration for sorting is " << fixed
                     << time_elapsed << setprecision(6) << " sec" << endl;
                cout << endl;
        }
        return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. What are the inputs to Excel’s RAND function? a.There is one argument to put into...
1. What are the inputs to Excel’s RAND function? a.There is one argument to put into the function – the distribution from which to generate random numbers. b.There are two arguments to put into the function – the number of random values to generate, followed by the number of decimal places. c.The RAND function has no arguments, just parentheses with nothing in between. 2. What do you actually type into a spreadsheet cell to use the RAND function? a. =RAND()...
#5. You can find 20 RANDOM NUMBERS in a Table or you can generate them with...
#5. You can find 20 RANDOM NUMBERS in a Table or you can generate them with software like Excel. The Excel functions are “RAND” and “RANDBETWEEN”. With “Randbetween” you simply input how many numbers you want, the number of digits you want in your random number and the range of values you want those numbers to fall between. For example you may want twenty, 2-digit numbers that fall between 00 and 100 (like “34”). TWO CONSIDERATIONS: (1) You must systematically...
Write program to generate random numbers in different range and display: (c) A number in range...
Write program to generate random numbers in different range and display: (c) A number in range [0, 10) (d) A number in range [0, 1000] (e) A number in range [-99, 99] (f) A double number in range [1000.0, 9999.0) In Java Please
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Generate 100 random numbers using the RAND function and create a frequency distribution and a histogram...
Generate 100 random numbers using the RAND function and create a frequency distribution and a histogram with bins of width 0.1. Apply the chi-square goodness of fit test (see Chapter 5) to test the hypothesis that the data are uniformly distributed. This question is from Business Analytics 3rd Edition by James R Evans and from Chapter 12 and question 1 The question is from following book and from Chapter 12 question 1 Textbook: James Evans, Business Analytics, 3nd edition, 2019,...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
In C++ Write a simple program to generate random integers between X and Y and store...
In C++ Write a simple program to generate random integers between X and Y and store them in a file, one number per line. The user should input the number of elements to generate and the boundary numbers X and Y (e.g. the inputs 100, 0, 999 mean your program should generate a list of 100 integers with values between 0 and 999, inclusive).
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT