this sorting technique is not working

why is this not working(selection sort)


void selsort(int a[], int n)
{ int low,flag,tmp;
for(int i=0;i<n/;i++)
{ flag=0;
for(int j=i;j<n;j++)
{
if((a[j]<a[i])&&(flag!=1))
{low=a[j];
for(int k=j; k<n;k++)
{ if(a[k]<low)
low=a[k];
if(k==n-1)
flag=1;
}
tmp=a[i];
a[i]=low;
low=tmp;
}
}
}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void selsort(int a[], int n)
{ 
           int low , flag , tmp ;
           
           for( int i = 0 ; i < n / ; i++)
           {
                   flag=0;
                   
                   for(int j=i;j<n;j++)
                   {
                           if( ( a[j] < a[i] ) && ( flag != 1 ) )
                           {
                                      low = a[j] ;
                                      
                                      for( int k = j ; k < n ; k++ )
                                      {
                                                if( a[k] < low )
                                          
                                                low = a[k] ;
                                   
                                                if( k == n-1 )

                                                flag=1;
                                      }

                                      tmp = a[i] ;

                                      a[i] = low ;

                                      low = tmp ;
                           }
                  }
        }
}


first foor loop i < n / ??? ;
Hello guddu232000,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
			}
		}
	}
}


Hope that helps,

Andy
Topic archived. No new replies allowed.