Question
#include #include #include using namespace std; int genrand(int minv, int maxv) { return (rand() % (maxv - minv) + minv); } void genrandNumber(int *numbers, int
#include
int genrand(int minv, int maxv) { return (rand() % (maxv - minv) + minv); }
void genrandNumber(int *numbers, int min_value, int max_value, int num) { cout << "Random #'s from 10-50" << endl; for (int i = 0; i < num; i++) { numbers[i] = genrand(min_value, max_value); cout << numbers[i] << " "; } cout << endl; }
int bubblesort(int *arr, int num) { int temp = 0; int swapCounter = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 9; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapCounter++; } } } return swapCounter; }
int main() { int arr[10]; int swapCounter; float sum = 0; int n; srand(time(NULL)); cout << "Enter the number of time you want sort numbers" << endl << "Average swap count" << endl; cin >> n; for (int i = 0; i < n; i++) {
genrandNumber(arr, 10, 50, 10); swapCounter = bubblesort(arr, 10); sum += swapCounter;
for (int i = 0; i < 10; i++) { cout << arr[i] << "\t"; } cout << endl; cout << "Total # of swaps " << swapCounter << endl; } cout << "Average # of swaps " << sum / n << endl; return 0; }
i did this but now i need to enhance my code and i am using C++: "After the first pass, the largest number is guaranteed to be in the highest indexed element in the array. After the second pass the two largest numbers are in place. Modify the bubble sort to make less comparisons on each pass, thus increasing the efficiency of the algorithm" i ned help thank you
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