Use quick sort method to sort data in increasing order (implemented in C language)

Use the recursive call of the quick sort method to achieve increasing order
(take notes for easy review)

#include <stdio.h>

int m,x,i,j;
int h[50];//顺序表最大存储

creatb(int h[])//创建顺序表
{
    
    
    printf("input data:\n");
    scanf("%d",&x);
    i=0;
    while(x!=0)
    {
    
    
        i++;
        h[i]=x;
        scanf("%d",&x);
    }
}

int parttion(int h[],int low,int high)//一次划分,成两段
{
    
    
    x=h[low];//取第一个值为枢轴
    while(low<high)//low=high时退出
    {
    
    
        while((low<high)&&(h[high]>=x))//找到high之前的第一个比枢轴x小的
            high--;
        if(low<high)
        {
    
    
            h[low]=h[high];//找到high之前的第一个比枢轴x小的新值high赋值给前面low
            low++;//可以不要,后面会有这一步
        }

        while((low<high)&&(h[low]<x))//找到low之后的第一个比枢轴x大的新值
            low++;
        if(low<high)
        {
    
    
            h[high]=h[low];//找到low之后的第一个比枢轴x大的新值low赋值给后面high
            high--;//可以不要
        }
    }
    h[low]=x;//将枢轴x赋值给low索引的值并返回
    return low;
}

quicksort(int h[],int low,int high)//递归快速排序
{
    
    
    if(low<high)
    {
    
    
        j=parttion(h,low,high);//j为一次排序后的枢轴
        quicksort(h,low,j-1);//前段:j-1作为high
        quicksort(h,j+1,high);//后段:j+1作为low
    }
}

outb(int h[])//输出顺序表
{
    
    
    for(m=1;m<=i;m++)
        printf("%4d",h[m]);
}

main()
{
    
    
    creatb(h);
    printf("排序前:\t");
    outb(h);
    quicksort(h,1,i);
    printf("\n排序后:\t");
    outb(h);
}

Please correct me!

Guess you like

Origin blog.csdn.net/weixin_46250447/article/details/106871593