Question

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 an ascending order selection
sort, modified to print out the array contents after each pass of the sort.

Output:

Bubble Sort
The unsorted values are: 7 2 3 8 4 5 6 1 
 sort pass #1 : 2 7 3 8 4 5 6 1 
 sort pass #2 : 2 3 7 8 4 5 6 1 
 sort pass #3 : 2 3 7 4 8 5 6 1 
 sort pass #4 : 2 3 7 4 5 8 6 1 
 sort pass #5 : 2 3 7 4 5 6 8 1 
 sort pass #6 : 2 3 7 4 5 6 1 8 
 sort pass #7 : 2 3 4 7 5 6 1 8 
 sort pass #8 : 2 3 4 5 7 6 1 8 
 sort pass #9 : 2 3 4 5 6 7 1 8 
 sort pass #10 : 2 3 4 5 6 1 7 8 
 sort pass #11 : 2 3 4 5 1 6 7 8 
 sort pass #12 : 2 3 4 1 5 6 7 8 
 sort pass #13 : 2 3 1 4 5 6 7 8 
 sort pass #14 : 2 1 3 4 5 6 7 8 
 sort pass #15 : 1 2 3 4 5 6 7 8 

The sorted values are: 1 2 3 4 5 6 7 8 

Selection Sort
The unsorted values are: 7 2 3 8 4 5 6 1 
 sort pass #1 : 1 2 3 8 4 5 6 7 
 sort pass #2 : 1 2 3 8 4 5 6 7 
 sort pass #3 : 1 2 3 8 4 5 6 7 
 sort pass #4 : 1 2 3 4 8 5 6 7 
 sort pass #5 : 1 2 3 4 5 8 6 7 
 sort pass #6 : 1 2 3 4 5 6 8 7 
 sort pass #7 : 1 2 3 4 5 6 7 8 

The sorted values are: 1 2 3 4 5 6 7 8 

Thank you

Homework Answers

Answer #1

#source code:

#include <iostream>

using namespace std;

void printA(int a[],int n){

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

cout<<a[i]<<" ";

}

cout<<endl;

}

void bsort(int a[],int n){

int s=1;

cout<<"The unsorted values are:";

printA(a,n);

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

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

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

int t=a[j];

a[j]=a[j+1];

a[j+1]=t;

cout<<"sort pass #"<<s<<": ";

s=s+1;

printA(a,n);

}

}

}

cout<<"The sorted values are:";

printA(a,n);

}

void ssort(int a[],int n){

int s=1;

cout<<"The unsorted values are:";

printA(a,n);

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

int p = c;

for (int d=c+1;d<n;d++){

if (a[p] > a[d])

p=d;

}

if (p!= c){

int t = a[c];

a[c] = a[p];

a[p] = t;

cout<<"sort pass #"<<s<<": ";

s=s+1;

printA(a,n);

}

}

cout<<"The sorted values are:";

printA(a,n);

}

int main(){

int a[8]={7,2,3,8,4,5,6,1};

cout<<"Bubble Sort"<<endl;

bsort(a,8);

cout<<"Selection Sort"<<endl;

int b[8]={7,2,3,8,4,5,6,1};

ssort(b,8);

return 0;

}

#output:

#if you have any doubt or more information needed comment below.i will respond as possible as soon.thanks.

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
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’...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random values for an array and sorted values; sorted the same list twice and collect time each time) for the following array sizes: 1000, 2000, and 10000. You should be able to confirm that the runtime is n^2 for unsorted list (i.e., going from 1000 to 2000 should be about 4 times slower and going from 1000 to 10000 should be about 100times slower).
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program. The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . .
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
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! 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
Write a program which: Write a program which uses the following arrays: empID: An array of...
Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to hold each employee’s gross salary....