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...
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...
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...
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,...
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...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
Write a C program that when you type in five numbers, it will print the number...
Write a C program that when you type in five numbers, it will print the number from the smallest one to the largest one.(if you type in 2, 1, 3, 5, 4, the output will be 1, 2, 3, 4, 5 ) You may need more than one function to complete this mission.
Write a C++ program that simulates flipping a coin (random numbers) 1000 times and displays the...
Write a C++ program that simulates flipping a coin (random numbers) 1000 times and displays the number of heads and tails. Note that there is no input to the program and the number of heads and tails will be around 500.
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...