Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

dou sbould have the heltionrtntuse the flnothe code to your directory cp /net/da

ID: 3887459 • Letter: D

Question


dou sbould have the heltionrtntuse the flnothe code to your directory cp /net/data/ftp/pub/class/170/ftp/cpp/BinarySearch/SelSortMain.cpp SelSortMain.cpp cp net/data/ftp/pub/class/170/ftp/cpp/Binarysearch/SelSort.h SelSort.h cp net/data/ftp/pub/class/170/ftp/cpp/BinarySearch/SelSort.cpp SelSort.cpp Your goal here will be to modify the code so that you are sorting in ascending order (smallest to largest) instead of descending order (largest to smallest). Hint: this will involve only changing one comparison in SelSort.cpp

Explanation / Answer

#include<iostream>

using namespace std;

int main()

{

int size, arr[50], i, j, temp;

cout<<"Enter The number of elements (Array Size) : ";

cin>>size;

cout<<"Enter values : ";

for(i=0; i<size; i++)

{

cin>>arr[i];

}

cout<<"Sorting(Ascending Order) the above list using selection sort... ";

for(i=0; i<size; i++)

{

for(j=i+1; j<size; j++)

{

//asending order chage > symbol to < for descening order.

if(arr[i]>arr[j])

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

cout<<"Sorted array. : ";

for(i=0; i<size; i++)

{

cout<<arr[i]<<" ";

}

return 0;

}

OUTPUT

Enter The number of elements (Array Size) : 5
Enter values : 11 60 22 5 10
Sorting(Ascending Order) the above list using selection sort...

Sorted array. :
5 10 11 22 60

Note:- If you want to sorted array in desceinding order just change this line if(arr[i]>arr[j]) to if(arr[i]<arr[j])