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 generates a random number between 1 and 50 and asks the user...
Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials it took to guess...
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 MIPS program to ask user to input the number of elements of array
write a MIPS program to ask user to input the number of elements of array
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",...
Write a method that displays every other element of an array. Write a program that generates...
Write a method that displays every other element of an array. Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.) Write two overloaded methods that return the average of an array with the following headers:      public static int average(int[] intArray)        public static double average(double[] dArray) Also,...
Write a program that dynamically allocates a built-in array large enough to hold a user-defined number...
Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept negative numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT