0
Follow
0
View

About the problem of #c language # : the implementation of the integer array sorting function, and indicate the use of sorting algorithm

dadmy3652013 注册会员
2023-02-28 21:21

Here's an example.

#include

void main()
{
    int i,a[10],temp,pos;
    printf("为数组元素赋值:\n");
    for(i=0;i<10;i++)
    {
        printf("a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=1;i<10;i++)
    {
        temp=a[i];
        pos=i-1;
        while((pos>=0) && (temppos]))
        {
            a[pos+1]=a[pos];
            pos--;
        }
        a[pos+1]=temp;
    }
    for(i=0;i<10;i++)
    {
        printf("%d\t",a[i]);
    }
}


cryice520 注册会员
2023-02-28 21:21

Change the value as follows:


#include
void sort(int *array, int array_num)
{
    int i,j,temp;
    for(i=0;i<array_num-1;i++)
        for(j=1;j<array_num-i;j++)
        {
            if(array[j]<array[j-1])
            {
                temp=array[j-1];
                array[j-1]=array[j];
                array[j]=temp;
            }
        }
}
int main()
{
    int array_num,i,j;
    printf("请输入要排序的数字个数:");
    scanf("%d",&array_num);
    int array[array_num]; // 数组定义放到下面
    printf("请输入要排序的数字:");
    for(i=0;i<array_num;i++)
    {
        scanf("%d",&array[i]);
    }
    
    sort(array, array_num);  // 排序函数调用修改
    
    for(j=0;j<array_num;j++)
    {
        printf("%d ",array[j]);
    }
    return 0; 
 
}

lijie43011 注册会员
2023-02-28 21:21
< div class = " md_content_show“数据- v - 3967 e397 = " " >

< div class = " aw-list-img " > img

About the Author

Question Info

Publish Time
2023-02-28 21:21
Update Time
2023-02-28 21:21