Write a C++ programs uses a simple selection sort (do NOT use any of the variati
ID: 3767410 • Letter: W
Question
Write a C++ programs uses a simple selection sort (do NOT use any of the variations) on a list of integers between 0 and 100. Note: recursion is NOT required for these exercises. The list cannot contain more than 100 entries. The selection sort should sort the integers is ascending order. Prompt the user for the name of a file containing an unsorted list of integers. The file might contain (without the commas): 100, 94, 59, 83, 7,11, 92, 76, 37, 89, 74, 59, 65, 79, 49, 89, 89, 75, 64, 82,15, 74, 82, 68, 92, 61, 33, 95, 91, 82, 89, 64, 43, 93, 86, 65, 72, 40,42, 90, 81, 62, 90, 89, 35, 81, 48, 33, 94, 81, 76, 86, 67, 70,100, 80, 83, 78, 96, 58 Use an array of integers to store the data. Place the sorted data in an output file with no more than 10 integers per line in orderly columns. Before exiting inform the user of the name of the output file, the number of integers in the input file, and the total number of swaps that were required to sort the file.Explanation / Answer
#include<iostream>
#include<conio.h>
using namespace std;
template <class T>
void s_sort(T a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"Enter The number of Element: ";
cin>>n;
cout<<" Enter Elements: ";
for(i=0;i<n;i++)
{
cout<<" Enter:";
cin>>a[i];
}
s_sort(a,n);
cout<<" After Sorting ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.