#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "adecbf";
int len = strlen(str);
printf("%s\n", str);
for (int i = 0; i < len; i++)
{
int min_index = i;
for (int j = i + 1; j < len; j++)
{
if (str[j] < str[min_index])
min_index = j;
}
str[i] ^= str[min_index];
str[min_index] ^= str[i];
str[i] ^= str[min_index];
}
printf("%s\n", str);
return 0;
}
输出结果有问题
原因 按位异或不能跟自己比 破坏了原数据
所以要把按位异或代码块加个if条件
if (i != min_index)
{
str[i] ^= str[min_index];
str[min_index] ^= str[i];
str[i] ^= str[min_index];
}
说完了 接着去踩坑!