Write a C# main program to compare the time spans for running 1,000 times on bot
ID: 3884775 • Letter: W
Question
Write a C# main program to compare the time spans for running 1,000 times on both sorting methods for randomly created 1,000 integers ranging from 100 to 1000. Use bubble sort and merge sort. Show the testing results for each of the two methods using a randomly created integer array of 50 elements.
Make sure that both of the methods work correctly.
Example Output
=================C# code and testing result for Sort Method>
individual C# main program includes only Sort One on 50-element array
=================C# code and testing result for Sort Method Two=================
individual C# main program includes Only Sort Two on 50-element array
=================C# code and running time spans for the two methods ==================
Include both Sorting methods in the program and run 1,000 times on 1000-element array
Explanation / Answer
using namespace std;
int main(){
//Seeds random number generator
srand(time(NULL));
//Declarations
int temp;
int tenBubbleComp = 0;
int tenBubbleSwap = 0;
int hunBubbleComp = 0;
int hunBubbleSwap = 0;
int thoBubbleComp = 0;
int thoBubbleSwap = 0;
int tenArray[10];
int hundredArray[100];
int thousandArray[1000];
//Populates a 10 element array with random numbers
for (int x = 0; x < 10; x++){
int y = rand() % 10;
tenArray[x] = y;
}
//Populates a 100 element array with random numbers
for (int x = 0; x < 100; x++){
int y = rand() % 100;
hundredArray[x] = y;
}
//Populates a 1000 element array with random numbers
for (int x = 0; x < 1000; x++){
int y = rand() % 1000;
thousandArray[x] = y;
}
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10 - 1; y++) {
if (tenArray[y]>tenArray[y + 1]) {
temp = tenArray[y + 1];
tenArray[y + 1] = tenArray[y];
tenArray[y] = temp;
tenBubbleSwap++;
}
}
tenBubbleComp++;
}
for (int x = 0; x < 100; x++) {
hunBubbleComp++;
for (int y = 0; y < 100 - 1; y++) {
if (hundredArray[y]>hundredArray[y + 1]) {
temp = hundredArray[y + 1];
hundredArray[y + 1] = hundredArray[y];
hundredArray[y] = temp;
hunBubbleSwap++;
}
}
}
for (int x = 0; x < 1000; x++) {
thoBubbleComp++;
for (int y = 0; y < 1000 - 1; y++) {
if (thousandArray[y]>thousandArray[y + 1]) {
temp = thousandArray[y + 1];
thousandArray[y + 1] = thousandArray[y];
thousandArray[y] = temp;
thoBubbleComp++;
}
}
}
cout << "Bubble Sort:" << endl << endl << endl;
cout << "10 Element Array" << endl;
cout << "Comparisons: " << tenBubbleComp << endl;
cout << "Swaps: " << tenBubbleSwap << endl << endl;
cout << "100 Element Array" << endl;
cout << "Comparisons: " << hunBubbleComp << endl;
cout << "Swaps: " << hunBubbleSwap << endl << endl;
cout << "1000 Element Array" << endl;
cout << "Comparison: " << thoBubbleComp << endl;
cout << "Swaps: " << hunBubbleSwap << endl << endl << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.