冒泡
#include <stdio.h>
void maopao(float A[])
{ int i,j;
float temp;
for (i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(A[j]<A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%g ", A[i]);
}
putchar(10);
return;
}
int main(int argc, const char *argv[])
{
float A[5] = {1.2, 5.6, 3.3, 2.4, 7.9};
maopao(A);
return 0;
}
选择
#include <stdio.h>
void xunaze(float A[])
{ int i,j;
float temp;
int idx;
for(i=0;i<4;i++)
{
idx = i;
for(j=i+1; j<=4; j++)
{
if( A[j] > A[idx] )
{
idx = j;
}
}
if(idx != i)
{
temp = A[i];
A[i] = A[idx];
A[idx] = temp;
}
}
for(i=0;i<5;i++)
{
printf("%g ",A[i]);
}
putchar(10);
return;
}
int main(int argc, const char *argv[])
{
float A[5] = {1.8, 9.6, 8.3, 6.4, 7.9};
xunaze(A);
return 0;
}