Home > Back-end >  From big to small, why my result is wrong
From big to small, why my result is wrong

Time:05-21

#include
Void main ()
{
15,34,6,27,48,4,20 int a [7]={}, I, j, k, t;
Printf (" before sorting sequence: ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);
for(i=0; i<7. I++)
{k=I;
for(j=i+1; J<7. J++)
If (a [j] & gt; A [k]) k=j;
if(k!=I) t=a, [k]. a[k]=a[i]; A [I]=t; }
Printf (" \ n from big to small sorted (select) \ n ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);
printf("\n");
}

CodePudding user response:

Modified as follows, for reference:
 # include 
Void main ()
{
15,34,6,27,48,4,20 int a [7]={}, I, j, k, t;
Printf (" before sorting sequence: ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);
for(i=0; i<7-1; I++)
{
k=i;
for(j=i+1; J<7. J++)
If (a [j] & gt; A [k]) k=j;
if(k!=I)
{
T=a, [k].
a[k]=a[i];
A [I]=t;
}
}
Printf (" \ n from big to small sorted (select) \ n ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);
printf("\n");

}

CodePudding user response:

 # include 


//void main ()
Int main ()
{
15,34,6,27,48,4,20 int a [7]={}, I, j, k, t;
Printf (" before sorting sequence: ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);
for(i=0; i<7. I++)
{
k=i;
for(j=i+1; J<7. J++)
If (a [j] & gt; A [k])
K=j;
# if 0
if(k! I)={
T=a, [k]. a[k]=a[i]; A [I]=t;
}
# the else
If (k!=j)
T=a [k], a [k]=[I], a [I]=t;
# endif
}
Printf (" \ n from big to small sorted (select) \ n ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);

printf("\n");

return 0;
}

For your reference ~

A little curly braces, or by a comma instead of a semicolon;

CodePudding user response:

You of the inner for loop in the second if part of an exchange of the three statements with curly braces, it is a whole

if(k! I)={
T=a, [k]. a[k]=a[i]; A [I]=t;
}

CodePudding user response:

reference 2 building self-confidence boy reply:
 # include 


//void main ()
Int main ()
{
15,34,6,27,48,4,20 int a [7]={}, I, j, k, t;
Printf (" before sorting sequence: ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);
for(i=0; i<7. I++)
{
k=i;
for(j=i+1; J<7. J++)
If (a [j] & gt; A [k])
K=j;
# if 0
if(k! I)={
T=a, [k]. a[k]=a[i]; A [I]=t;
}
# the else
If (k!=j)
T=a [k], a [k]=[I], a [I]=t;
# endif
}
Printf (" \ n from big to small sorted (select) \ n ");
for(i=0; i<7. I++)
Printf (" % 5 d, "a [I]);

printf("\n");

return 0;
}

For your reference ~

A little curly braces, or by a comma instead of a semicolon;

The if (k!=j) into the if (k!=I) because the initial k=I;

CodePudding user response:

If (k!=I)
reference
without braces
 
For (I=0; i <7. I++)
{
K=I;
For (j=I + 1; J & lt; 7. J++)
If (a [j] & gt; A [k])
k=j;
If (k!=I)
{
T=a, [k].
A [k]=a, [I].
A [I]=t;
}
}

  • Related