Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include using namespace std; int genrand(int minv, int maxv) { return (rand() % (maxv - minv) + minv); } void genrandNumber(int *numbers, int

#include #include #include using namespace std;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago