selection sorting

Hi,
I was trying to do selection sort for the array data[5]={3,1,4,7,5}. But always '7' comes as the first element and the rest of it is sorted correctly. the program I had written was this:

Note: Please let me know the mistake in this rather than giving a new program for sorting. My main goal is to know my mistake here. Thanks in advance :)

int selsort(int data[], int n)
{
int temp,min_id=0,min=0,i;
for(i=0; i<=n-1; i++)
min=data[i];
{
for (int j=i+1; j<n; j++)
if(data[j]<min)
{
min=data[j];
min_id=j;
}
temp=data[i];
data[i]=data[min_id];
data[min_id]=temp;

}
//for printing the elements:
for (int k =0; k<=n-1; k++)
{
cout << data[k]<<endl;
}
cin.get();
return 0;
}


can someone please help?
Thanks...
closed account (j3bk4iN6)
I can tell you its more readable using the code tags from the format: menu just paste highlight and press the source code button It creates a window that indents
Last edited on
closed account (j3bk4iN6)


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
int selsort(int data[], int n)
{
	int temp ,min_id = 0 ,min = 0 , i;

	for(i = 0; i <= n-1; i++)
	           min = data[i];

	for(int j = i+1; j<n; j++)
	{//check here
		if(data[j] < min)
		{
			min = data[j];
			min_id = j;
		}
		temp = data[i];
		data[i] = data[min_id];
		data[min_id] = temp;

	}
	//for printing the elements:
	for(int k = 0; k <= n-1; k++)
	{
		cout << data[k] << endl;
	}
	cin.get();
	return 0;
}
Last edited on
1
2
3
4
5
for(i = 0; i <= n-1; i++)
	           min = data[i];

// here you make min the last entry in data[i] = (n-1)
// min becomes every element in the array, each time overriding the last 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int j = i+1; j<n; j++)
	{//check here
		if(data[j] < min)
		{
			min = data[j];
			min_id = j;
		}
		temp = data[i];
		data[i] = data[min_id];
		data[min_id] = temp;

	}
// here you initialize j to i+1, which will be the last iteration of the last loop, so j = n;
// then say while j < n which won't be true so this loop skips 


1
2
3
4
5
6
//for printing the elements:
	for(int k = 0; k <= n-1; k++)
	{
		cout << data[k] << endl;
	}
// here I'd say it simply prints out the array since you have done nothing with it. 


Moral of the story, brackets are important :)
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
int selsort(int data[], int n)
{
   int temp,min_id=0,min=0,i;
   for(i=0; i<=n-1; i++)
   {  
      min=data[i];

      for (int j=i+1; j<n; j++)
      {
         if(data[j]<min)
         {
            min=data[j];
            min_id=j;
         }
         temp=data[i];
         data[i]=data[min_id];
         data[min_id]=temp;
      }
   }

   //for printing the elements:
   for (int k =0; k<=n-1; k++)
      cout << data[k]<<endl;

   cin.get();
   return 0;
}
// this may work, it's your code with brackets included as required.
// but is untested 


http://www.algolist.net/Algorithms/
What's even more impressive on that page is the visualizers, there's java code that you can run step by step and see exactly what every variables value is, you can also set it to run at certain speeds 1-10.
This is extremely helpful to run through it in your head after doing the reading and doing the step by step as well, to make sure your knowledge is sound.

http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html

Last edited on
Topic archived. No new replies allowed.