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
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Design a program that allows the user to enter 5 names into a String array. Sort...
Design a program that allows the user to enter 5 names into a String array. Sort the array in ascending (alphabetical) order and display its contents. Search the array for a specific name that the user wants. (python)
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).
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 . . .
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
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]
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...
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’...
Write a Java program using the OOP paradigm to do the following: 1. Main Function: create...
Write a Java program using the OOP paradigm to do the following: 1. Main Function: create a list with a million items, and you can use the generate function to add a million random values to the list. 2. Create a method to sort the list created in the main function using bubble sort, then use the sorted list to search for the (k) value using binary search. 3. Create a method to create a new list of 1000 items...
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;   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT