Files
2023-02-21 10:56:54 +08:00

33 lines
839 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <stdio.h>
void fun(int a[], int n, int flag) {
int t, i, j, k;
for (i = 0; i < n - 1; i++) {
k = i;
for (j = i + 1; j < n; j++) {
if (flag ? a[k] > a[j]: a[k] < a[j]) {
k = j;
}
if (k != i) {
t = a[k];
a[k] = a[i];
a[i] = t;
// Swap a[k] and a[i]
}
}
}
}
int main() {
int c[10] = {8, 6, 7, 10, 9, 3, 5, 2, 4, 1}, i;
fun(c, 5, 0);
//fun(c + 5, 5, 1);
for (i = 0; i < 10; i++) {
printf("%d, ", c[i]);
}
//9, 10, 8, 7, 6, 1, 2, 3, 4, 5,
//一个莫名其妙的排序,它不一定能给出正确的顺序。
//老师出这个题的意义可能只是想强调c是一个指针加5就代表着c[5]的地址?
return 0;
}