即输即排的冒泡排序法,可部分排对,原因何在

 //用动态内存分配实现冒泡排序
#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;
}

先把冒泡排序函数写好,用的时候调用就行了

冒泡排序本就是函数,随着元素增多,每次都执行就好啦~