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
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
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...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
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...
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’...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT