selecting sorting in array problem

for(i=0;i<5;i++)
{
cin>>arry[i];
}

for(i=0;i<4;i++)
for(j=i;j<4-i;j++)
{
if(arry[i]>arry[j+1])
temp=arry[i];
arry[i]=arry[j+1];
arry[j+1]=temp;
}
for(i=0;i<5;i++)
{
cout<<arry[i];
}
But it print wrong values why?
Are overwriting there any?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for ( int i = 0; i < 4; i++ )
{
   int min = i;
   for ( int j = i + 1; j < 5; j++ )
   {
      if ( arry[min] < arry[j] ) min = j;
   }

   if ( min != i )
   {
      int tmp = arry[i];
      arr[i] = arr[min];
      arr[min] = tmp;
   }
}
why are use these
 
min = i;

why program wrong without it
At first we suppose that the minimum element is the first element of the sub sequence. So we remember its position that to compare this element with other elements in the subsequence.
Last edited on
Topic archived. No new replies allowed.