Sir, when we sort the array using Selection sort, we call the function on line 32. We pass the array using (pass by value) and not pass by reference. So on displaying the array in main how the values of arrays get updated?
It must remain same as a copy of array is made in the function of Selection Sort. Also the function is not returning anything.
Thank you.
How values are updated in array?
Please send the code as well.
#include
using namespace std;
void selection_sort(int ar[],int n)
{
for(int i=0; i<n-1; i++)
{
int min_index = i;
for(int j=i+1; j<=n-1; j++)
{
if(ar[j] < ar[min_index])
{
min_index = j;
}
}
swap(ar[min_index],ar[i]);
}
}
int main()
{
int n;
cout << " ENTER THE NUMBER OF ELEMENTS OF ARRAY : ";
cin >> n;
int ar[n];
for(int i=0; i<n; i++)
{
cin >> ar[i];
}
selection_sort(ar,n);
for(int i=0; i<n; i++)
{
cout << ar[i] << " ";
}
cout << endl;
return 0;
}
Integer arrays are always passed by reference by default. So the changes done to an array in a function are reflected in int main() as well.
1 Like