Question

In: Computer Science

Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize)...

Sorting Benchmarks
Write a program that generates and sorts an array of a user-specified number (arraySize) of randomly generated numbers. To keep the values to a reasonable range by using the array size as the upper bound for the random numbers (between 1 and arraySize). Your program should call the individual functions that implement the five sorting algorithms discussed in class (see the lecture slides). Each function should keep a count of the number of comparisons/exchanges it makes. Display the pre-sorted array, the sorted array, and the number of comparisons for each algorithm.

Note: In this assignment, you will compare all the five sorting algorithms from the lecture notes (brute force sort, bubble sort, bubble sort++, selection sort, and insertion sort) with each other using the same data for each. Start with getting one to work first. The idea is that once you have one, adding in the other algorithms is relatively simple. Hint: use the algorithms from the slides! Your program MUST include a function for each algorithm and use them appropriately. The main procedural code must first prompt the user for the arraySize = number of elements to store in the array ("How many elements in the array?"). Then, it will generate arraySize random integers between 1 and arraySize (the number they entered--yes, you will use it twice!). Since each function will sort the array, changing the values, you will need to make multiple copies of the generated array to test the same numbers against each algorithm. Each algorithm will return the number of comparisons made. This is when one element is compared to another, regardless of whether or not they get swapped. NO GLOBAL VARIABLES!

You will need to include at least these functions:
void displayArray(int values[], int size);
int bruteForceSort(int values[], int size);
int bubbleSort(int values[], int size);
int bubblePPSort(int values[], int size);
int selectionSort(int values[], int size);
int insertionSort(int values[], int size);

The output should look something like this -- user inputs are in bold blue type:
How many elements in the array? 20
BRUTE FORCE SORT
Before sorting:
10 14 6 7 12 18 9 13 4 17 19 2 17 2 4 10 20 3 9 14
After sorting:
2 2 3 4 4 6 7 9 9 10 10 12 13 14 14 17 17 18 19 20
Number of comparisons: 400

BUBBLE SORT
Before sorting:
10 14 6 7 12 18 9 13 4 17 19 2 17 2 4 10 20 3 9 14
After sorting:
2 2 3 4 4 6 7 9 9 10 10 12 13 14 14 17 17 18 19 20
Number of comparisons: 190

BUBBLE SORT PLUS PLUS
Before sorting:
10 14 6 7 12 18 9 13 4 17 19 2 17 2 4 10 20 3 9 14
After sorting:
2 2 3 4 4 6 7 9 9 10 10 12 13 14 14 17 17 18 19 20
Number of comparisons: 184

SELECTION SORT
Before sorting:
10 14 6 7 12 18 9 13 4 17 19 2 17 2 4 10 20 3 9 14
After sorting:
2 2 3 4 4 6 7 9 9 10 10 12 13 14 14 17 17 18 19 20
Number of comparisons: 206

INSERTION SORT
Before sorting:
10 14 6 7 12 18 9 13 4 17 19 2 17 2 4 10 20 3 9 14
After sorting:
2 2 3 4 4 6 7 9 9 10 10 12 13 14 14 17 17 18 19 20
Number of comparisons: 186

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <cstdlib>

using namespace std;

const int max_size = 50;

void displayArray(int values[], int size);

int bruteForceSort(int values[], int size);

int bubbleSort(int values[], int size);

int bubblePPSort(int values[], int size);

int selectionSort(int values[], int size);

int insertionSort(int values[], int size);

int main()

{

    int values1[max_size], values2[max_size], values3[max_size], values4[max_size], values5[max_size];

    int size;

    cout << "How many elements in the array? ";

    cin >> size;

    for (int i = 0; i < size; i++)

    {

        values1[i] = (rand() % (size - 1)) + 1;

        values2[i] = values1[i];

        values3[i] = values1[i];

        values4[i] = values1[i];

        values5[i] = values1[i];

    }

    cout << "\nBRUTE FORCE SORT" << endl;

    cout << "Before sorting:" << endl;

    displayArray(values1, size);

    int comp = bruteForceSort(values1, size);

    cout << "After sorting:" << endl;

    displayArray(values1, size);

    cout<<"Number of comparisons: "<<comp<<endl;

    cout << "\nBUBBLE SORT" << endl;

    cout << "Before sorting:" << endl;

    displayArray(values2, size);

    comp = bubbleSort(values2, size);

    cout << "After sorting:" << endl;

    displayArray(values2, size);

    cout<<"Number of comparisons: "<<comp<<endl;

    cout << "\nBUBBLE SORT PLUS PLUS" << endl;

    cout << "Before sorting:" << endl;

    displayArray(values3, size);

    comp = bubblePPSort(values3, size);

    cout << "After sorting:" << endl;

    displayArray(values3, size);

    cout<<"Number of comparisons: "<<comp<<endl;

    cout << "\n\nSELECTION SORT" << endl;

    cout << "Before sorting:" << endl;

    displayArray(values4, size);

    comp = selectionSort(values4, size);

    cout << "After sorting:" << endl;

    displayArray(values4, size);

    cout<<"Number of comparisons: "<<comp<<endl;

    cout << "\n\nINSERTION SORT" << endl;

    cout << "Before sorting:" << endl;

    displayArray(values5, size);

    comp = insertionSort(values5, size);

    cout << "After sorting:" << endl;

    displayArray(values5, size);

    cout<<"Number of comparisons: "<<comp<<endl;

    return 0;

}

void displayArray(int values[], int size)

{

    for (int i = 0; i < size; i++)

    {

        cout << values[i] << " ";

    }

    cout << endl;

}

int bruteForceSort(int values[], int size){

    int comparisions = 0;

    for(int i=0; i<size; i++){

        for(int j=0; j<size; j++){

            comparisions++;

            if(values[i]<values[j]){

                int temp = values[i];

                values[i] = values[j];

                values[j] = temp;

            }

        }

    }

    return comparisions;

}

int bubbleSort(int values[], int size){

    int comparisions = 0;

    for(int i=0; i<size; i++){

        for(int j=0; j<size-i-1; j++){

            comparisions++;

            if(values[j]>values[j+1]){

                int temp =values[j];

                values[j] = values[j+1];

                values[j+1] = temp;

            }

        }

    }

    return comparisions;

}

int bubblePPSort(int values[], int size)

{

    int comparisons=0;

    for (int i = 0; i < size; i++)

    {

        int flag = 0;

        for (int j = 0; j < size - i; j++)

        {

            comparisons++;

            if (values[j] > values[j + 1])

            {

                flag = 1;

                int temp = values[j];

                values[j] = values[j + 1];

                values[j + 1] = temp;

            }

        }

        if (flag == 0)

            break;

    }

    return comparisons;

}

int selectionSort(int values[], int size)

{

    int comparisons = 0;

    for (int i = 0; i < size-1; i++)

    {

        for (int j = i+1; j < size; j++)

        {

            comparisons++;

            if (values[i] > values[j])

            {

                int temp = values[i];

                values[i] = values[j];

                values[j] = temp;

            }

        }

    }

    return comparisons;

}

int insertionSort(int values[], int size)

{

    int i, key, j, comparisons = 0;

    for (i = 1; i < size; i++)

    {

        key = values[i];

        j = i - 1;

        while (j >= 0 && values[j] > key)

        {

            comparisons++;

            values[j + 1] = values[j];

            j = j - 1;

        }

        values[j + 1] = key;

    }

    return comparisons;

}


Related Solutions

In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified maximum. Display the total amount of numbers printed.
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. 18. Enhance the program that you wrote for Programming...
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
// JavaLanguage . You need to write a program that asks the user for an array...
// JavaLanguage . You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.
Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT