Question

Please answer in C++ with the correct files (in bold). Thanks! Write a program that creates...

Please answer in C++ with the correct files (in bold). Thanks!

  1. 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.
  2. Please use the file names listed below since your file will have the following components:
    Ch18_Ex15.cpp
    searchSortAlgorithms.h

Homework Answers

Answer #1

//Source Code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void bubbleSort(int arr[], int n, int &comp, int &assignments){
assignments = 1; // by assigning i=0
for(int i=0;i<n-1;i++){
++comp; // by comparing (i<n-1)
++assignments; // by assigning j=0
for(int j=0;j<n-1-i;j++){
++comp; // by condition (j<n-1-i)
if(arr[j] > arr[j+1]){
++comp;// by comparing (arr[j] > arr[j+1])
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
assignments += 3; // by above three statements
}
++comp; // when (arr[j] > arr[j+1]) condition fails
}
++comp; // when (j<n-1-i) condition fails
}
++comp; // when (i<n-1) condition fails
}


void selectionSort(int arr[], int n, int &comp, int &assignments){
assignments = 1; // by assigning i=0
for(int i=0; i<n-1; i++){
++comp; // by condition (i<n-1)
int min = arr[i];
int pos = i;
assignments += 3; // from above two statements and from j=i+1 below
for(int j=i+1;j<n;j++){
++comp;// by comparing (j<n)
if(arr[j] < min){
++comp; // by comparing (arr[j] < min)
min = arr[j];
pos = j;
assignments += 2; // from above two statements
}
++comp; // when if condition fails
}
++comp; // when condition (j<n) fails

int temp = arr[i];
arr[i] = min;
arr[pos] = temp;
assignments += 3; // by above three statements
}
++comp; //when (i<n-1) condition fails
}


void insertionSort(int arr[],int n, int &comp, int &assignments){
int i, j, temp;
assignments = 1; // by assigning i=1
for (i = 1; i < n; i++){
++comp;// by condition (i<n)
temp = arr[i];
j = i-1;
assignments += 2; // by above two statements
while (j >= 0 && arr[j] > temp){
comp += 2; // by (j >= 0) and (arr[j] > temp)
arr[j+1] = arr[j];
j = j-1;
assignments += 2; // by above two statements
}
comp += 2; //when condition (j >= 0) and condition (arr[j] > temp) fails

arr[j+1] = temp;
assignments++; // by above statement
}
comp++; //when (i<n) condition fails
}

void fillArray(int list[], int length)
{
srand(time(0));
for (int i = 0; i < length; i++){
list[i] = rand() % 20000;
}
}

void copyArray(int list1[], int list2[], int n)
{
for (int i = 0; i < n; i++){
list2[i] = list1[i];
}
}


int main()
{
int list1[5000];
int list2[5000];
int list3[5000];

int compBubbleSort = 0, compSelectionSort = 0, compInsertionSort = 0;
int assignBubbleSort = 0, assignSelectionSort = 0, assignInsertionSort = 0;

fillArray(list1, 5000);
copyArray(list1, list2, 5000);
copyArray(list1, list3, 5000);

bubbleSort(list1, 5000, compBubbleSort, assignBubbleSort);
selectionSort(list2, 5000, compSelectionSort, assignSelectionSort);
insertionSort(list3, 5000, compInsertionSort, assignInsertionSort);

cout << "Number of comparisons---" << endl;
cout << " Bubble sort: " << compBubbleSort << endl;
cout << " Selection sort: " << compSelectionSort << endl;
cout << " Insertion sort: " << compInsertionSort << endl << endl;

cout << "Number of item assignments---" << endl;
cout << " Bubble sort: " << assignBubbleSort << endl;
cout << " Selection sort: " << assignSelectionSort << endl;
cout << " Insertion sort: " << assignInsertionSort << endl << endl;

return 0;
}

//Output Screenshot :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
This program is in C++, And please consider " sort pass #" for the output: Write...
This program is in C++, And please consider " sort pass #" for the output: Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...