Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A Race between Selection sort and Bubble sort Implement both selection sort and

ID: 3724995 • Letter: A

Question

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() s struct rusage ruse; getrusage (RUSAGE SELF, &ruse;); return ruse.ru utime.tv sec+ruse.ru utime.tv usec/1000000.0 ruse.ru_stime.tv_sectruse.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.

Explanation / Answer

void bubbleSort(string array[], int size)

{

string temp;

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

{

if (array[i] > array[i + 1])

{

temp = array[i];

array[i] = array[i + 1];

array[i + 1] = temp;

}

}

return;

}

void selectionSort(string array[], int size)

{

int index;

string smallest;

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

{

index = i;

smallest = array[i];

for (int x = 1; x < size; x++)

{

if (array[x] < smallest)

{

smallest = array[x];

index = x;

}

}

array[index] = array[i];

array[i] = smallest;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote