QUESTION
Sort the given set of numbers using Selection Sort. The first line of the input contains the number of elements, the second line of the input contains the numbers to be sorted. In the output print the status of the array at the 4th iteration and the final sorted array in the given format.
“TESTCASE_1”: “5\n25 47 11 65 1\n###—###SEPERATOR—###—\n25 47 11 65 1 \n1 47 11 65 25 \n1 11 47 65 25 \n1 11 25 65 47 \n1 11 25 47 65 \nSorted Array:1 11 25 47 65”, “TESTCASE_2”: “7\n14 83 25 47 9 77 1\n###—###SEPERATOR—###—\n14 83 25 47 9 77 1 \n1 83 25 47 9 77 14 \n1 9 25 47 83 77 14 \n1 9 14 47 83 77 25 \n1 9 14 25 83 77 47 \n1 9 14 25 47 77 83 \n1 9 14 25 47 77 83 \nSorted Array:1 9 14 25 47 77 83”, “TESTCASE_3”: “8\n14 83 25 47 9 77 1 0\n###—###SEPERATOR—###—\n14 83 25 47 9 77 1 0 \n0 83 25 47 9 77 1 14 \n0 1 25 47 9 77 83 14 \n0 1 9 47 25 77 83 14 \n0 1 9 14 25 77 83 47 \n0 1 9 14 25 77 83 47 \n0 1 9 14 25 47 83 77 \n0 1 9 14 25 47 77 83 \nSorted Array:0 1 9 14 25 47 77 83 “, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0
ANSWER
#include <iostream>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sortArray(int a[],int n){
int max_idx,max=-99999;
for(int i=0;i<n-1;i++){
max_idx=i;
for(int j=i+1;j<n;j++){
if(a[j]<a[max_idx]){
max_idx=j;
}
}
swap(&a[max_idx],&a[i]);
if(i<=n){
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
cout<<endl;
}
}
}
int main()
{
int n;
cin>>n;
int a[n];
for(int j=0;j<n;j++){
cin>>a[j];
cout<<a[j]<<" ";
}
cout<<endl;
sortArray(a,n);
cout<<"Sorted Array:";
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
return 0;
}