//用动态内存分配实现冒泡排序
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
printf("Please input n: ");
int n;
scanf("%d",&n);
//为数组p动态分配n个整数类型大小的空间
int* p;
if (!(p = (int*)calloc(n,sizeof(int))))
{
printf("Fail to allocate the memory.\n");
exit(1);
}
printf("Please input %d number: ",n);
for (int i = 0; i < n; i++)
{
scanf("%d",p+i);
for (int j = i;j >= 0; j--)
{
if (*(p + i) < *(p + j))
{
int t = 0;
t = *(p + i);
*(p + i) = *(p + j);
*(p + j) = t;
}
}
}
printf("After sort, the number is show as follow: \n");
for (int i = 0; i < n; i++)
{
printf("%6d",p[i]);
}
printf("\n");
system("pause");
return 0;
}