#include <iostream>
usingnamespace std;
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void exchangeSort(int n, int S[])
{
int i, j;
for (i = 1; i <= n - 1; i++)
for (j = i + 1; j <= n; j++)
if (S[j] < S[i])
swap(i, j);
}
int main()
{
int i;
int S[] = { 10, 5, 3, 7, 21, 15, 2, 11, 4, 20 };
exchangeSort(10, S);
for (int i = 0; i < 10; i++)
cout << S[i] << " ";
system("pause");
return 0;
}
You're passing by value instead of passing by reference or pointer. When you pass by value, a local copy of the argument variable is created which would cease to exit the moment the function terminates. By passing by reference or pointer, you can give the function direct access to the argument variable. You may also use the const keyword when you're passing by reference or pointers to restrict what the function can do with the variables being passed to it.
Lastly, don't get into the habit of using 'or equal to' in your loops. Instead, always make sure that your loops are counting from index 0 onward.
Please don't delete your post. It makes this thread useless as a learning resource for others. Please respect the other users of this forum, and restore your original post.