Write a function that findsreturns the index of the maximum element in the array
ID: 3616920 • Letter: W
Question
Write a function that findsreturns the index of the maximum element in the array.
int MaxIndex(int arr[], intsize)
Use the above function toimplement selectionSort, i.e.,
void selectionSort(intarr[], int size)
This is an example of howselection sort works
Take the array and find themaximum in thearray: arr = 3 4 5 91 (maximum = 9)
Place the maximum value atthe lastindex arr = 3 4 5 19 (9 is at correct place)
find the maximum in thearray of size 4 arr = 3 4 5 19 (maximum = 5)
Place the maximum at lastindex-1 arr = 3 4 1 59 ( 5 and 9 are at correct place)
find maximum in the arrayof size3 arr = 3 4 1 59 (maximum = 4)
place the maximum at lastindex-2 arr = 3 1 4 59 (4,5,9 are at correct place)
Keep repeating the abovesteps till array is sorted
Also write the mainfunction to test your program.
Explanation / Answer
please rate - thanks #include using namespace std; int MaxIndex(int arr[], int size) {int i,max; max=0; for(i=1;iarr[max]) max=i; return max; } void selectionsort(int a[], int n) { int i,max,temp,k; for(i=n-1;i>=1; i--) {max=MaxIndex(a,i); temp=a[i]; a[i]=a[max]; a[max]=temp; } } int main() {int a[6]={55, 34, 56, 76, 5, 10}; int i; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.