Question
Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The program then sorts list1 using bubble sort, list2 using
Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm.
Please use the file names listed below since your file will have the following components: Ch18_Ex15.cpp searchSortAlgorithms.h
BASED ON THE ABOVE GUIDELINES HOW WOULD I MAKE THE FOLLOWING CODE WORK FOR SORTING 5000 ELEMENTS INSTEAD OF THE 10 SPECIFIED???
//Header file//
searchSortAlgorithms.h
void swap(int *xp, int *yp);
void bubbleSort(int arr[], int n);
void selectionSort(int arr[], int n);
void insertionSort(int arr[], int n);
void printArray(int arr[], int size);
//Main//
Ch18_Ex15.cpp
#include
#include
#include "searchSortAlgorithms.h"
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j, comparison = 0;
for (i = 0; i
// Last i elements are already in place
for (j = 0; j
if (arr[j] > arr[j+1]){
comparison += 1;
swap(&arr[j], &arr[j+1]);
}
}
}
cout
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx, comparison = 0;
// One by one move boundary of unsorted subarray
for (i = 0; i
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j
if (arr[j]
min_idx = j;
}
comparison += 1;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
cout
}
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j, comparison = 0;
for (i = 1; i
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
comparison += 1;
arr[j+1] = key;
}
cout
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i
cout
cout
}
// Driver program to test above functions
int main()
{
int list1[] = {64, 34, 25, 12, 22, 11, 90, 76, 90, 100};
int list2[] = {64, 34, 25, 12, 22, 11, 90, 76, 90, 100};
int list3[] = {64, 34, 25, 12, 22, 11, 90, 76, 90, 100};
int n = sizeof(list1)/sizeof(list1[0]);
bubbleSort(list1, n);
cout
printArray(list1, n);
selectionSort(list2, n);
cout
printArray(list2, n);
insertionSort(list3, n);
cout
printArray(list3, n);
return 0;
}
Sample output:
C:\WINDOWS system32\cmd.exe Number of Comparison Made using Bubble Sort Algorithm is: 15 orted array using Bubble Sort is: 12 25 34 64 76 90 90 100 Number of Comparison Made using Selection Sort Algorithm is: 45 orted array using Selection Sort is: 12 25 34 64 76 90 90 100 Number of Comparison Made using Insertion Sort Algorithm is: 9 orted array us ing Insertion Sort is 12 25 34 64 76 90 90 100 Press any key to continueStep 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