Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using Unix(PuTTy) : A Race between Selection sort and Bubble sort Implement both selection sort and bubble sort, so that they sort an array of
using Unix(PuTTy) :
A Race between Selection sort and Bubble sort Implement both selection sort and bubble sort, so that they sort an array of strings into ascending order. Accept a number form the command line, and create two identical copies of an array of random ten character strings of that length. Measure how long selection sort takes to sort one array. Measure how long bubble sort takes to sort the other copy. Repeat the experiment for a number of different array sizes, and provide some meaningful conclusions about the algorithms' relative speeds. Here is a special timing function you can include. #include#include double get_cpu_time() { struct rusage ruse; getrusage(RUSAGE_SELF, &ruse); return ruse.ru_utime.tv_sec+ruse.ru_utime.tv_usec/1000000.0 + ruse.ru_stime.tv_sec+ruse.ru_stime.tv_usec/1000000.0; } Example run: a.out 10000 It took 3.179 seconds for selection sort to sort 10000 strings It took 3.330 seconds for bubble sort to sort the same strings.
-----------------------------------------------------------------------------------------------------------
complete on this code please
#include#include #include #include using namespace std; double get_cpu_time() { struct rusage ruse; getrusage(RUSAGE_SELF, &ruse); return ruse.ru_utime.tv_sec+ruse.ru_utime.tv_usec/1000000.0 + ruse.ru_stime.tv_sec+ruse.ru_stime.tv_usec/1000000.0; } void selection_sort(string A[], int N) { int sum = 0; for (int i = 0; i < N; i += 1) for (int j = 0; j < N; j += 1) sum += 1; } void bubble_sort(string A[], int N) { int sum = 0; for (int i = 0; i < N; i += 1) for (int j = 0; j < i; j += 1) sum += 1; } string random_string() { string s = ""; for (int i = 0; i < 10; i += 1) s += "abcdefghijklmnopqrstuvwxyz"[random() % 26]; return s; } int main(int argc, char * argv[]) { srandomdev(); cout << "The command line contained: "; for (int i = 0; i < argc; i += 1) cout << i << ": " << argv[i] << " "; int N = atoi(argv[1]); string * A1 = new string[N]; for (int i = 0; i < N; i += 1) A1[i] = random_string(); string * A2 = new string[N]; for (int i = 0; i < N; i += 1) A2[i] = A1[i]; double t1 = get_cpu_time(); selection_sort(A1, N); double t2 = get_cpu_time(); bubble_sort(A2, N); double t3 = get_cpu_time(); cout << "selection sort took " << t2-t1 << " seconds "; cout << "bubble sort took " << t3-t2 << " seconds "; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started