PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can us the preview button at the bottom to see how it looks.
As I was looking at your code I started making comments, so here it is instead of a long message.
void selsort(int a[], int n)
{
int low, flag, tmp; // <--- low and flag not really needed. Initalize your variables.
for (int i = 0; i < n / ; i++) // <--- What is the / for?
{
flag = 0;
for (int j = i; j < n; j++) // <--- should be n -1 to stay inside the bounds of the array.
{
if ((a[j]<a[i]) && (flag != 1)) // <--- Once flag = 1 this statement will always be false.
{
low = a[j]; // Lines 11 - 18 I am thinking that you are over complicating
for (int k = j; k<n; k++) // your work. using low here and after the for loop is going about
{ // it the wrong way. Not sure why you set flag to 1, but it will
if (a[k]<low) // be a problem later.
low = a[k];
if (k == n - 1)
flag = 1;
}
tmp = a[i]; // Wrong way to implement the swap. you need to be swapping array elements not
a[i] = low; // not an rray element and variable.
low = tmp; // Point this line makes a variable = to temp when it should be an array element.
// code is most often written as
//tmp = a[j];
//a[j] = a[j + 1];
//a[j + 1] = tmp;
}
}
}
}