Question: #include using namespace std; //function to display an array void display(int *a, int n) { cout 0) { cout = n || j = n)
#include using namespace std;
//function to display an array void display(int *a, int n) { cout 0) { cout = n || 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 a[swapIdx])) swapIdx = j; } if(i != swapIdx) //swap { swaps++; swap(a, n, i, swapIdx); swapchar(s, n, i , swapIdx); } } cout a[j+1]) || (!up && a[j]
// 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 0 && ((up && a[j] a[j-1]))) { comparisons ++; swaps++; swap(a, n, j-1, j); swapchar(s, n, j-1, j); j= j - 1; } } cout
// 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>type>>n>>sortUp; for(int i=0;i>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


![n) //check if index is in range return; char temp = a[i];](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66eff9498089d_95366eff94904075.jpg)
![a[i] = a[j]; a[j] = temp; } // The swap function takes](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66eff94a16a19_95366eff949a90df.jpg)
using my code anove how do i fix it to run as teacher expected . this is c++
Leave manO the The correct output before any input should be way it is written, it first calls selectionSort on a sample array to help you debug your code and verify that it works swapped 0 and 4 and array is now [0,1,3,2,4] swapped 2 and 3 and array is now [e,1,2,3,4) swaps 2, comparisons-10 abcdefghij after that, the code descendmg/descreasing order or 1 for ascending increasing, and then a set of numbers in the array waits for input, first the name of a sorting algorithm, then the size of an array up to 10, then a 0 for For example if you copy paste this input below selection 6 0 57649 3 (so selection sort, 6 stems, sort down, 15,764,9.3) you should get this output swapped 9 and 5 and array is now [9,7,6,4,5,3] swapped 5 and 4 and orray is now [9,7,6,5,4,3] swaps 2, comparisons-15 ebcadfghij If instead you copy paste the same imput but with sort up (1) selection 6 1 576 4 9.3 you should get this output 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 13,4,5,6,9,71 swapped 7 and 9 and array is now [3,4,5,6,7,9] swaps-5, comparisons 15 fdacbeghij would nstead be insertion 6 you should get this output 5 7.6 493 swapped 7 and 6 and array is now [5,6,7,4,9, 31 swapped 7 and 4 and areay is no [5,6,4,7,9,3] insertion sort would instead be insertion 6 1 57649 3 you should get this output swapped 7 and 6 and array is now [5,6,7,4,9,3) swapped 7 and 4 and array is now [5,6,4,7,9,31 swapped 6 and 4 and array is now [5,4,6,7,9,3] wapped 5 and 4 and array is now [4,5,6,7,9,3] swapped 9 and 3 and array is now [4,5,6,7,3,9] swapped 7 and 3 and array is now [4,5,6,3,7,9] wapped 6 and 3 and array is now [4,5,3,6,7,9] swapped 5 and 3 and array is now [4,3,5,6,7,9) swapped 4 and 3 and array is now [3,4,5,6,7,9] swaps 9, comparisons-9 fdacbeghij
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
