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

QI1 (5 marks) Write a C++ program Which asks the user to inter 10 integers and t

ID: 3871223 • Letter: Q

Question

QI1 (5 marks) Write a C++ program Which asks the user to inter 10 integers and then print the numbers in ascending order. Use selection sort algorithm to sort the list of numbers. (Hint: in the selection sort, the list is divided into two parts, a sorted part and a non-sorted part. The algorithm searches the for the minimum value and swap it with the top element in it. After the swapping, the top element now belongs to the sorted part, hence, the unsorted part is reduced by one element) non-sorted list

Explanation / Answer

#include<iostream>

using namespace std;

int main()
{
int i,j,n;

int currmin,temp,min;

int a[10];
n=10; // Number of elements
cout<<" Enter the elements ";

for(i=0;i<10;i++)
{
cout<<"Enter the " << i << " th element : ";
cin>>a[i];
}

for(i=0;i<n-1;i++)
{
min=a[i];
currmin=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
currmin=j;
}
}

temp=a[i];
a[i]=a[currmin];
a[currmin]=temp;
}

cout<<" Sorted list : ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}

return 0;
}