my code coplier is cpp/ c++ #include <iostream> using namespace std; // write th
ID: 3596778 • Letter: M
Question
my code coplier is cpp/ c++
#include <iostream>
using namespace std;
// write the code that implemets the functions below
// The swapchar doesn't print or return any value, just swaps the 2
// characters at index i and j in the array a of size n.
void swapchar(char *a,int n,int i,int j){
// insert your code here
}
// The swap function takes in an array a[], of size n,
// and swaps the content of the array at indexes i and j.
// It returns false if there is no swap (i.e. if i and j are the same)
// and true otherwise.
// If there is a swap, for example swapping the first two elements in
// [4,3,2,1] it also prints:
// swapped 3 and 4 and array is now [3,4,2,1]
bool swap(int *a,int n,int i,int j) {
// insert your code here
return true;
}
// write a function called selectionSort that sorts an array of ints
// a, of size n, either in descending order (e.g., 5,4,3...) when up
// is true, or in ascending order (e.g., 3,4,5...) when up is false.
// Every time a swap is made, it should add 1 to the variable swaps
// (note that if swap() returns false, it should not increase swaps).
// Every time a comparison is made, it should add 1 to the variable
// comparisons. At the end it should print swaps and comparisons.
// Also, every time elements in the int array a is swapped, the same
// elements in the char array s should also be swapped.
// The sorting algorithm should be Selection Sort.
void selectionSort(int*a,char*s,int n,bool up){
int comparisons=0,swaps=0;
// insert your code here
cout<<"swaps="<<swaps<<",comparisons="<<comparisons<<endl;
}
// Write a function with the same requirements as selectionSort,
// but the sorting algorithm should be Bubble Sort.
void bubbleSort(int*a,char*s,int n,bool up){
int comparisons=0,swaps=0;
// insert your code here
cout<<"swaps="<<swaps<<",comparisons="<<comparisons<<endl;
}
// Write a function with the same requirements as selectionSort,
// but the sorting algorithm should be Insertion Sort.
void insertionSort(int*a,char*s,int n,bool up){
int comparisons=0,swaps=0;
// insert your code here
cout<<"swaps="<<swaps<<",comparisons="<<comparisons<<endl;
}
// leave the code below in main as it is, write code only in the above functions
int main() {
int a[10]={4,1,3,2,0};
char s[]="ebdcafghij";
string type="selection";
int n=5;
//simple hardcoded test for debugging
bool sortUp=true;
selectionSort(a,s,5,true);
cout<<s<<endl;
cin>>type>>n>>sortUp;
for(int i=0;i<n;i++){cin>>a[i];}
if (type=="selection") {
selectionSort(a,s,n,sortUp);
} else if (type=="bubble") {
bubbleSort(a,s,n,sortUp);
} else if (type=="insertion") {
insertionSort(a,s,n,sortUp);
}
cout<<s<<endl;
return 0;
}
Explanation / Answer
Given below is the completed code. When executed the program waits for user input of hte following format
<sortname> <noofelements> <sortorder> <elements ......>
where sortname is one of following values - selection, bubble, insertion
sortorder is 0 for descending and 1 for ascending order
noofelements - specifies how many elements will be input after sortorder
<elements...> is the actual list of numbers to be sorted.
for example, the line selection 6 1 5 7 6 4 9 3
indicates , selection sort is to be usd, on 6 elements , ascending order, and the elements to be sorted are 5 7 6 4 9 3
===============
I have tested the code with these input lines ....
selection 6 0 5 7 6 4 9 3
selection 6 1 5 7 6 4 9 3
insertion 6 1 5 7 6 4 9 3
bubble 6 1 9 2 8 3 5 6
Hope the answer helped. If it did, please don't forget to rate it . Thank you.
#include <iostream>
using namespace std;
//function to display an array
void display(int *a, int n)
{
cout << "[" ;
if(n > 0)
{
cout << a[0];
for(int i = 1; i < n; i++)
cout << ", " << a[i];
}
cout << "]" << endl;
}
// write the code that implemets the functions below
// The swapchar doesn't print or return any value, just swaps the 2
// characters at index i and j in the array a of size n.
void swapchar(char *a,int n,int i,int j){
// insert your code here
if( i < 0 || i >= n || j < 0 || j >= n) //check if index is in range
return;
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
// The swap function takes in an array a[], of size n,
// and swaps the content of the array at indexes i and j.
// It returns false if there is no swap (i.e. if i and j are the same)
// and true otherwise.
// If there is a swap, for example swapping the first two elements in
// [4,3,2,1] it also prints:
// swapped 3 and 4 and array is now [3,4,2,1]
bool swap(int *a,int n,int i,int j) {
// insert your code here
if(i == j)
return false;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
cout << "swapped " << a[i] << " and " << a[j] << " and array is now ";
display(a, n);
return true;
}
// write a function called selectionSort that sorts an array of ints
// a, of size n, either in descending order (e.g., 5,4,3...) when up
// is true, or in ascending order (e.g., 3,4,5...) when up is false.
// Every time a swap is made, it should add 1 to the variable swaps
// (note that if swap() returns false, it should not increase swaps).
// Every time a comparison is made, it should add 1 to the variable
// comparisons. At the end it should print swaps and comparisons.
// Also, every time elements in the int array a is swapped, the same
// elements in the char array s should also be swapped.
// The sorting algorithm should be Selection Sort.
void selectionSort(int*a,char*s,int n,bool up){
int comparisons=0,swaps=0;
// insert your code here
int swapIdx;
for(int i = 0; i < n; i++)
{
swapIdx = i;
for( int j = i + 1; j < n; j++)
{
comparisons++;
if((up && a[j] < a[swapIdx]) || (!up && a[j] > a[swapIdx]))
swapIdx = j;
}
if(i != swapIdx) //swap
{
swaps++;
swap(a, n, i, swapIdx);
swapchar(s, n, i , swapIdx);
}
}
cout<<"swaps="<<swaps<<",comparisons="<<comparisons<<endl;
}
// Write a function with the same requirements as selectionSort,
// but the sorting algorithm should be Bubble Sort.
void bubbleSort(int*a,char*s,int n,bool up){
int comparisons=0,swaps=0;
// insert your code here
for(int i = 0; i < n-1; i++)
{
for(int j = 0; j < n-i-1; j++)
{
comparisons++;
if((up && a[j] > a[j+1]) || (!up && a[j] < a[j+1]))
{
swaps++;
swap(a, n, j, j+1);
swapchar(s, n, j, j+1);
}
}
}
cout<<"swaps="<<swaps<<",comparisons="<<comparisons<<endl;
}
// Write a function with the same requirements as selectionSort,
// but the sorting algorithm should be Insertion Sort.
void insertionSort(int*a,char*s,int n,bool up){
int comparisons=0,swaps=0;
// insert your code here
for( int i = 0 ;i < n ; i++ )
{
int j = i ;
while( j > 0 && ((up && a[j] < a[ j-1]) || (!up && a[j] > a[j-1])))
{
comparisons ++;
swaps++;
swap(a, n, j-1, j);
swapchar(s, n, j-1, j);
j= j - 1;
}
}
cout<<"swaps="<<swaps<<",comparisons="<<comparisons<<endl;
}
// leave the code below in main as it is, write code only in the above functions
int main() {
int a[10]={4,1,3,2,0};
char s[]="ebdcafghij";
string type="selection";
int n=5;
//simple hardcoded test for debugging
bool sortUp=true;
selectionSort(a,s,5,true);
cout<<s<<endl;
cin>>type>>n>>sortUp;
for(int i=0;i<n;i++){cin>>a[i];}
if (type=="selection") {
selectionSort(a,s,n,sortUp);
} else if (type=="bubble") {
bubbleSort(a,s,n,sortUp);
} else if (type=="insertion") {
insertionSort(a,s,n,sortUp);
}
cout<<s<<endl;
return 0;
}
output
amoeba-2:Test2 raji$ ./a.out
swapped 0 and 4 and array is now [0, 1, 3, 2, 4]
swapped 2 and 3 and array is now [0, 1, 2, 3, 4]
swaps=2,comparisons=10
abcdefghij
selection 6 0 5 7 6 4 9 3
swapped 9 and 5 and array is now [9, 7, 6, 4, 5, 3]
swapped 5 and 4 and array is now [9, 7, 6, 5, 4, 3]
swaps=2,comparisons=15
ebcadfghij
amoeba-2:Test2 raji$ ./a.out
swapped 0 and 4 and array is now [0, 1, 3, 2, 4]
swapped 2 and 3 and array is now [0, 1, 2, 3, 4]
swaps=2,comparisons=10
abcdefghij
selection 6 1 5 7 6 4 9 3
swapped 3 and 5 and array is now [3, 7, 6, 4, 9, 5]
swapped 4 and 7 and array is now [3, 4, 6, 7, 9, 5]
swapped 5 and 6 and array is now [3, 4, 5, 7, 9, 6]
swapped 6 and 7 and array is now [3, 4, 5, 6, 9, 7]
swapped 7 and 9 and array is now [3, 4, 5, 6, 7, 9]
swaps=5,comparisons=15
fdacbeghij
amoeba-2:Test2 raji$ ./a.out
swapped 0 and 4 and array is now [0, 1, 3, 2, 4]
swapped 2 and 3 and array is now [0, 1, 2, 3, 4]
swaps=2,comparisons=10
abcdefghij
insertion 6 1 5 7 6 4 9 3
swapped 6 and 7 and array is now [5, 6, 7, 4, 9, 3]
swapped 4 and 7 and array is now [5, 6, 4, 7, 9, 3]
swapped 4 and 6 and array is now [5, 4, 6, 7, 9, 3]
swapped 4 and 5 and array is now [4, 5, 6, 7, 9, 3]
swapped 3 and 9 and array is now [4, 5, 6, 7, 3, 9]
swapped 3 and 7 and array is now [4, 5, 6, 3, 7, 9]
swapped 3 and 6 and array is now [4, 5, 3, 6, 7, 9]
swapped 3 and 5 and array is now [4, 3, 5, 6, 7, 9]
swapped 3 and 4 and array is now [3, 4, 5, 6, 7, 9]
swaps=9,comparisons=9
fdacbeghij
amoeba-2:Test2 raji$ ./a.out
swapped 0 and 4 and array is now [0, 1, 3, 2, 4]
swapped 2 and 3 and array is now [0, 1, 2, 3, 4]
swaps=2,comparisons=10
abcdefghij
bubble 5 1 2 8 3 5 6
swapped 3 and 8 and array is now [2, 3, 8, 5, 6]
swapped 5 and 8 and array is now [2, 3, 5, 8, 6]
swapped 6 and 8 and array is now [2, 3, 5, 6, 8]
swaps=3,comparisons=10
acdebfghij
amoeba-2:Test2 raji$ ./a.out
swapped 0 and 4 and array is now [0, 1, 3, 2, 4]
swapped 2 and 3 and array is now [0, 1, 2, 3, 4]
swaps=2,comparisons=10
abcdefghij
bubble 6 1 9 2 8 3 5 6
swapped 2 and 9 and array is now [2, 9, 8, 3, 5, 6]
swapped 8 and 9 and array is now [2, 8, 9, 3, 5, 6]
swapped 3 and 9 and array is now [2, 8, 3, 9, 5, 6]
swapped 5 and 9 and array is now [2, 8, 3, 5, 9, 6]
swapped 6 and 9 and array is now [2, 8, 3, 5, 6, 9]
swapped 3 and 8 and array is now [2, 3, 8, 5, 6, 9]
swapped 5 and 8 and array is now [2, 3, 5, 8, 6, 9]
swapped 6 and 8 and array is now [2, 3, 5, 6, 8, 9]
swaps=8,comparisons=15
bdefcaghij
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.