Write a program to selection sort, the program needs to repeatedlyprompt for inp
ID: 3614939 • Letter: W
Question
Write a program to selection sort, the program needs to repeatedlyprompt for input.Hint:
// precondition: n >= 0, and a has at least n elements
// postcondition: a[0] .. a[n-1] are "sorted" --i.e.,
// elements have been rearranged to be innondecreasing order
void selectionSort in a[], const in n);
// precondition: n >= 1, n is size of array a
// postcondition: return value is index in a of largestelement
int findIndexOfLargest (const int a[], const in n);
// precondition: none
// postcondition: values of a and b have been exchanged
void swap(int& a, int& b);
Explanation / Answer
//Hope this will helpyou.#include<iostream> using namespace std; void swap(int &a,int &b) { int temp; temp = a; a=b; b=temp; } int findIndexOfLargest (const int a[], const int n) { int m=0; int i; for(i=0;i<n;i++) if(a[m]<a[i]) m=i; return m; } void selectionSort (int a[],const int n){ int i; if(n<=0) return; for(i=0;i<n;i++) if(a[0] < a[i]) swap(a[0],a[i]); selectionSort(&a[1],n-1); } int main(){ int i; int b[]={1,2,6,4,2}; cout<<"Largest index is:"<<findIndexOfLargest(b,5)<<endl; selectionSort(b,5); for(i=0;i<5;i++) cout<<" "<<b[i]; system("pause"); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.