Question

In: Computer Science

C++!!! PLEASE PAY ATTENTION TO THE BOLD TEXT VERY WELL. THANK YOU Write a program that...

C++!!!

PLEASE PAY ATTENTION TO THE BOLD TEXT VERY WELL. THANK YOU

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

Solutions

Expert Solution

Code:

Ch18_Ex15.cpp

Code as text:

#include <iostream>

#include <cstdlib>

#include "searchSortAlgorithms.h"

using namespace std;

#define size 5000

// function to create three identical arrays

// elements range from 0 - 10000

void CreateList(int A[], int B[], int C[]) {

for (int i = 0; i < size; i++) {

A[i] = rand() % 10000;

B[i] = A[i];

C[i] = B[i];

}

}

int main() {

// declare three lists

int list1[size], list2[size], list3[size];

// create list using function

CreateList(list1, list2, list3);

// variables to count comparisons and assignments

int noOfComparisions, noOfAssignments;

// sort and output the result

cout << "Sorting list1 using bubble sort\n";

BubbleSort(list1, size, noOfComparisions, noOfAssignments);

cout << "Number of comparisons: " << noOfComparisions << endl;

cout << "Number of item assignments: " << noOfAssignments << endl;

cout << "\nSorting list2 using selection sort\n";

SelectionSort(list2, size, noOfComparisions, noOfAssignments);

cout << "Number of comparisons: " << noOfComparisions << endl;

cout << "Number of item assignments: " << noOfAssignments << endl;

cout << "\nSorting list3 using insertion sort\n";

InsertionSort(list3, size, noOfComparisions, noOfAssignments);

cout << "Number of comparisons: " << noOfComparisions << endl;

cout << "Number of item assignments: " << noOfAssignments << endl;

return 0;

}

searchSortAlgorithms.h

Code as text:

#ifndef _SEARCHSORTALGORITHMS_

#define _SEARCHSORTALGORITHMS_

using namespace std;

// function to sort using bubble sort

void BubbleSort(int arr[], int n, int &comp, int &assg) {

comp = assg = 0;

int i, j;

for (i = 0; i < n-1; i++) {

for (j = 0; j < n-i-1; j++) {

comp++; // increase comp by 1

if (arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

assg += 3; // 3 assignments

}

}

}

}

// function to sort using selection sort

void SelectionSort(int arr[], int n, int &comp, int &assg) {

comp = assg = 0;

int i, j, min_idx;

for (i = 0; i < n-1; i++) {

min_idx = i;

assg++; // 1 assignment

for (j = i+1; j < n; j++) {

comp++; // 1 comparison

if (arr[j] < arr[min_idx]) {

min_idx = j;

assg++; // 1 assignment

}

}

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

assg += 3; // 3 assignments

}

}

// function to sort using insertion sort

void InsertionSort(int arr[], int n, int &comp, int &assg) {

comp = assg = 0;

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

assg += 2; // 2 assignments

comp += 2; // 2 comparisons

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

assg += 2; // 2 assignments

}

arr[j + 1] = key;

assg++; // 1 assignment

}

}

#endif

Sample run:


Related Solutions

Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
Please write this code in C++, also if you could please bold the names of the...
Please write this code in C++, also if you could please bold the names of the input files and provide comments for each choice. For this part, the program gives the user 4 choices for encrypting (or decrypting) the first character of a file. Non-lowercase characters are simply echoed. The encryption is only performed on lowercase characters. If c is char variable, then islower(c) will return true if c contains an lowercase character, false otherwise To read a single character...
Please Answer with C code and I will rate! Thank you. Problem) Write a program that...
Please Answer with C code and I will rate! Thank you. Problem) Write a program that reads 20 integers and stores them in two arrays of 10 elements each. Then, the program should check if the two arrays have common elements, if yes, the program should displays the value of each common element and its position in both arrays. Otherwise, it should display a message and that their elements are different.
In c++ please. Write a program that opens a specified text tile then displays a list...
In c++ please. Write a program that opens a specified text tile then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
In c++ please. Write a program that reads the contents of two text files and compares...
In c++ please. Write a program that reads the contents of two text files and compares them in the following ways: It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the first file, but not the second. It should display a list of the words that appears in the second file,...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
Write a C++ program which performs a rot 13 substitution. Must be in C++. Thank you.
Write a C++ program which performs a rot 13 substitution. Must be in C++. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT