Sort and Search Evaluation - C++ You are to compare two sorting algorithms and c
ID: 3681947 • Letter: S
Question
Sort and Search Evaluation - C++You are to compare two sorting algorithms and compare two searching algorithms by running the algorithms and collecting data on each. Your data for sorting and searching will be strings of 15 characters in length. You are to have your program run, compute, and print all the results in one run, not multiples runs. (You are to run your program once for all data sets).
First part:
The two sorts to compare are the Bubble Sort and the Selection Sort. You are to test your sorts against different sets of strings. Each sort will sort six sets of data, 1000 strings, 3000 strings, 5000, 7000, 9000, and 11000 strings. You will compare how well each sort did with the different data sizes and show results. I would like to see a plot of the results. A table format showing results will do. Use the 'time' function and the 'difftime' function to gather sorts times. Show your answers with at least 4 places to the right of the decimal point.
Second part:
The two searches to use for comparisons are the Linear Search and the Binary Search. You will search for 3000 random strings in the array of 11000 strings array only and compute the average number of probes (not time!) needed to find a match. The target string will be a randomly selected string from the 11000 string's data set. You will randomly select 3000 strings for testing each search algorithm. Output the average number of probes needed for each search algorithm.
Third part:
How does your two search algorithms compare with the theoretical values?
Explanation / Answer
Part 1: See the program below. It provides functions for bubble sort and selection sort to sort an array of strings. Main function shows how to use the function and measure their running time on a set of test data. You can similarly use this to be run on your data and can measure time accordingly.
--------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//bubble sort function to sort an array of strings in descending order
void bubble_sort(string data[], int size) {
for (int i = 0; i < size; i++) {
for (int j = size - 1; j >= i; --j) {
if (data[j].compare(data[j + 1]) < 0) {
string temp;
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
//selection sort function to sort an array of strings in descending order
void selection_sort(string data[], int size) {
int i, j, max;
for (i = 0; i < size - 1; i++) {
for (j = i + 1, max = i; j < size; j++)
if (data[j].compare(data[max]) > 0)
max = j;
string temp;
temp = data[max];
data[max] = data[i];
data[i] = temp;
}
}
/*
*
*/
int main(int argc, char** argv) {
int size = 17;
string data[20] = {"Collins, Bill", "Smith, Bart", "Allet, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill",
"Aliison, Jeff", "Weaver, Jim", "Pore, Bob",
"Rutherford, Greg", "Javens, Renee",
"Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};
string data1[20] = {"Collins, Bill", "Smith, Bart", "Allet, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill",
"Aliison, Jeff", "Weaver, Jim", "Pore, Bob",
"Rutherford, Greg", "Javens, Renee",
"Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};
/*cout<<"Unsorted array for bubble sort..."<<endl;
for(int i=0;i<size;i++)
{
cout<<data[i]<<endl;
}*/
time_t start_time, end_time;
cout << "Going to execute bubble sort..." << endl;
start_time = time(0);
cout << "Start time for bubble sort:" << start_time << endl;
bubble_sort(data, size);
end_time = time(0);
cout << "End time for bubble sort:" << end_time << endl;
cout << "Time taken by bubble sort:" << difftime(end_time, start_time) << endl;
cout << "Going to execute selection sort..." << endl;
start_time = time(0);
cout << "Start time for selection sort:" << start_time << endl;
selection_sort(data1, size);
end_time = time(0);
cout << "End time for selection sort:" << end_time << endl;
cout << setprecision(6);
cout << "Time taken by selection sort:" << difftime(end_time, start_time) << endl;
/*cout<<"Sorted array..."<<endl;
for(int i=0;i<size;i++)
{
cout<<data[i]<<endl;
}*/
return 0;
}
-----------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.