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
#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.
Get Answers For Free
Most questions answered within 1 hours.