Question

Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes:

void setRandomValues(int[], int[], int length);

This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument arrays to a random value between(which includes) 1 and the array length value. When the function is done, both the first and second argument arrays should have exactly the same(random) values.

void bubbleSort(int[], int length);

This function performs a bubble sort on the argument integer array of the argument length size. When the function is done, the argument array will be sorted. You can use the sorting C++ code in the textbook here.

void selectionSort(int[], int length);

This function performs a selection sort on the argument integer array of the argument length size. When the function is done, the argument array will be sorted. You can use the sorting C++ code in the textbook here.

void compareBubbleAndSelectionSort(int length);

This function performs the following actions:

  1. Prints that it is comparing sorting functions for a list of size length.
  2. Calls setRandomValues to generate two identical random arrays of the argument length size.
  3. Uses the ctime library to get the current time in seconds(time(0)) and saves it.
  4. Calls bubbleSort on the first random array.
  5. Again uses the ctime library to get the current time in seconds(time(0) and saves it.
  6. Prints the number of seconds it took to bubble sort the array.
  7. Uses the ctime library to get the current time in seconds(time(0) and saves it.
  8. Calls selectionSort on the second random array.
  9. Again uses the ctime library to get the current time in seconds(time(0) and saves it.
  10. Prints the number of seconds it took to selection sort the array.

The output of the function should look something like this:

Comparing bubble and selection for a list of size 25000

Bubble sort took 3 second(s).

Selection sort took 1 second(s).

The program should then call compareBubbleAndSelectionSort with 25000, 50000, and 100000 length values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your required code in C++

#include <bits/stdc++.h>

using namespace std;

// to swap two values
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
  
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
  
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

void selectionSort(int arr[], int n)
{
int i, j, min_idx;
  
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
  
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}

// generate two arrays with random values
void setRandomValues(int a[], int b[], int length)
{
srand(time(0));
  
for(int i=0;i<length;i++)
{
//generate randm value from 1 to length
int x = rand()%length + 1;
a[i] = x;
b[i] = x;
}

}

// to compare time used by two different sorts
void compareBubbleAndSelectionSort(int length)
{
// output
cout<<"Comparing bubble and selection for a list of size "<<length<<endl;
  
int a[length], b[length];
  
//settting random values for both arrays
setRandomValues(a,b, length);
time_t curtime;
  
int t1, t2;
  
//storing time before sorting
t1 = time(0);
bubbleSort(a, length);
//storing time after sorting
t2 = time(0);
//output the time difference
cout<<"Bubble sort took "<<t2-t1<<" second(s)."<<endl;
  
//storing time before sorting
t1 = time(0);
selectionSort(b, length);
//storing time after sorting
t2 = time(0);
//output the time difference
cout<<"Selection sort took "<<t2-t1<<" second(s)."<<endl;
}

//driver code
int main()
{
//testing for different values of length
cout<<endl;
compareBubbleAndSelectionSort(25000);
cout<<endl;
compareBubbleAndSelectionSort(50000);
cout<<endl;
compareBubbleAndSelectionSort(100000);
  
return(0);
}

Please refer to the screenshot of the code to understand the indentation of the code:


Here is the Output for the sample Test Cases:

Add a comment
Know the answer?
Add Answer to:
Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

    Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

  • Qi. Create a java project Question that includes: • A bubble sort method to sort your...

    Qi. Create a java project Question that includes: • A bubble sort method to sort your arrays. Implement the below recursive binary search. • A java main class where you: o Create an array of characters that contains the letters of your first name followed by your last name, without spaces. o Call the sorting method to sort the array o Call binary search method to find the position of the first letter 'a' in your array. // Recursive Binary...

  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT