WHY AM I GETTING THE ERROR :

/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

#include
#include
using namespace std;

void b_sort(long int n, int a[])
{
for(long int i=0 ; i<n-1 ; i++ )
{
for(long int j=0 ; j<n ; j++)
{
if(a[j]>a[j+1])
{
swap (a[j],a[j+1]);
}
}
}

for(long int i=0 ; i<n ; i++)
cout<<a[i]<<endl;

}

int main() {
long int n;
int a[10000000];

cin>>n;
for(long int i=0 ; i<n ; i++)
{
	cin>>a[i];
}
b_sort(n,a);


return 0;

}

make the array a of size n only and in the function b_sort, j will go only till n-1.
Corrected code with comments

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.