C語言,簡單冒泡排序排10個整數,分別按照升序降序輸出,請問怎麼實現?
void foo(int * data, int n, int desc)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i; j++)
{
if ((data[j + 1] < data[j] && desc) || (data[j + 1] > data[j] && !desc))
{
int t = data[j + 1];
data[j + 1] = data[j];
data[j] = t;
}
}
}
}
int main()
{
int data[] = { 3, 2, 3, 4, 1, 9, 5, 2, 6, 8 };
for (int i = 0; i < 10; i++)
printf("%d ", data[i]);
printf("\n");
foo(data, 10, 0);
for (int i = 0; i < 10; i++)
printf("%d ", data[i]);
printf("\n");
foo(data, 10, 1);
for (int i = 0; i < 10; i++)
printf("%d ", data[i]);
printf("\n");
}