Revise the code for selectionSort in such a way that the program selects the nex
ID: 3693738 • Letter: R
Question
Revise the code for selectionSort in such a way that the program selects the next largest (instead of the next smallest) entry in the array and sorts the array into ascending order.
For example, If the input array is 9 7 3 4 1 8 6 2
Pass 1: 2 7 3 4 1 8 6 9 (the largest element (9) is in its final location)
Pass 2: 2 7 3 4 1 6 8 9 (the next largest element (8) is in its final location)
Pass 3: 2 6 3 4 1 7 8 9 (the next largest element (7) is in its final location
Etc. 2.
Explanation / Answer
Program for selection sort to arrange array elements in asscending order.
#include <stdio.h>
int main()
{
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
{
if(data[steps]>data[i])
/* To sort in descending order, change > to <. */
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.