enter image description here
I try to solve this problem. I checked the code logically but I couldn’t find the mistake
when i write this code on online compiler it worked
// selection sort code
#include <bits/stdc++.h>
using namespace std;
void selectionsort(int arr[],int n){
for(int i=0;i<=n-2;i++){
int min=i;
for (int j=i;j<=n-1;j++){
if(arr[j]<arr[min])
min=j;
}
swap(arr[i],arr[min]);
}
}
int swap(int a, int b){
int temp=a;
a=b;
b=temp;
return a,b;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
selectionsort(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
I was trying to solve this in gfg
New contributor
Jamsheed Mk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2