Question
Program Specification Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort
Program Specification Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on the screen.
The following is my code and the bold is where the errors are, If someone can help me claer these errors out of the way I'd appreciate it
#include
// SelectionSort array
int SelectionSort(int *arr, int len) { // Declarations for BubbleSort
int i, x, first, temp; int count = 0; // Locate First
for (i = len - 1; i > 0; i--) { first = 0;
// Locate smallest
for (x = 1; x <= i; x++) { if (arr[x] < arr[first]) first = x; } // Swap smallest
temp = arr[first]; arr[first] = arr[i]; arr[i] = temp; count++; } return count; } // BubbleSort array
int BubbleSort(int arr, int len) { // Declarations for BubbleSort
int i, x; int temp; int count = 0; // For statement i=1
for (i = 1; i < len; i++) { // Set x=0, less than length
for (x = 0; x < len - 1; x++) { // If statement
if (arr[x + 1] > arr[x]) { temp = arr[x]; arr[x] = arr[x + 1]; arr[x + 1] = temp; count++; } } } // *Count number of exchanges*
return count; } int main() { // Initialize arrays/and length of arrays
int arr1[20],arr2[20]; int len = 20;
for (int i = 0; i < len; i++) { arr1[i] = arr2[i] = rand() % 100; } //Set arrays = to bubble/selection sort
int bubbleSortCount = BubbleSort(arr1, len); int selectionSortCount = SelectionSort(arr2, len);
// Display number of exchanges
cout << "Bubble sort exchanges: " << bubbleSortCount << endl; cout << "Selection sort exchanges: " << selectionSortCount << endl;
// Add spacing
cout << " ";
return 0; }
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