输入n个数的数组 对数组进行从大到小排序 交换数组写出数组交换函数,这里我使用了冒泡排序
编码如下:
void chapter1::testSwap()
{
printf_s("swap 开始\r\n");
int n;
printf_s("请输入数组长度\r\n");
scanf_s("%d",&n);
int *a = (int *)calloc(n, sizeof(int));
for (size_t i = 0; i < n; i++)
{
printf_s("请输入第%d个数",i);
scanf_s("%d", &a[i]);
}
Swap(a, n);
for (size_t i = 0; i < n; i++)
{
printf_s("第%d个数是:%d\r\n",i, a[i]);
}
}
void chapter1::Swap(int a[],int alength)
{
for (size_t i = 0; i < alength; i++)
{
for (size_t j = 0; j < alength-1-i; j++)
{
if (a[j]<a[j+1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}