方法一:
#include <stdio.h>
int main(void)
{
char str[] ={1,3,5,7,9,20,2,4,6,8,0,13,14,15,16,17,18,19,20,20,21};
char i,j,k;
k = sizeof(str)/sizeof(char);//求出数组个数
for(i = 0; i < k; i++){
for(j = i+1;j < k;j++){
if(str[i] < str[j]){//判断满足交换条件否
str[i]^= str[j];
str[j]^= str[i];
str[i]^= str[j];
}
}
}
for(i = 0; i < k; i++){//打印
printf("str[i] = %d \n",str[i]);
}
}
方法二:
#include <stdio.h>
int main()
{
int a[3][4] = {1,3,5,7,9,2,4,6,8,0,17,11};
int *p = (int *)&a;
int i = 0,j = 0;
for(i = 0; i < 11; i++)
{
for(j = 0; j < 11 - i; j++)
{
if(p[j] < p[j+1])
{
p[j] ^= p[j+1];
p[j+1] ^= p[j];
p[j] ^= p[j+1];
}
}
}
for(i = 0; i < 3 ; i++)
{ for(j = 0; j < 4; j++)
{
printf("%d\t",a[i][j]);
}
putchar('\n');
}
return 0;
}