qfedu-c-level/day9/d4.c

30 lines
669 B
C

#include <stdio.h>
int main()
{
int arr[10] = {4, 2, 1, 5, 6, 7, 8, 9, 10, 3};
int *p = arr;
int *q = NULL; // 创建一个空指针,用于存放最小值的地址
while (p < &arr[9])
{
q = p + 1;
while (q <= &arr[9]) // 找出本轮最小值
{
if (*p > *q)
{
*p = *p ^ *q;
*q = *p ^ *q;
*p = *p ^ *q;
// printf("---> %d vs %d\n", *p, *q);
}
q++;
}
p++; // 指向下一个元素
}
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}