Selection sort arrays

include
using namespace std;

void selectionsort(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
int minIndex=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[minIndex])
{
minIndex=j;
}
}
swap(a[minIndex],a[i]);
}
}
void print(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
int main() {
int a[]={};
int n=sizeof(a)/sizeof(int);
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
selectionsort(a,n);
print(a,n);
return 0;
}
i am getting run time error

Error is because in your code you have not defined size of array a. First take input n from user than dynamically initialize array a as int *a=new int[n]. Then your code will run fine.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.