C语言模拟实现标准库函数之qsort()

#include 

void sort(int arr[],int left ,int right)
{
    if(left > right)
        return;

    int i = left;
    int j = right;
    int get = arr[left];

    while(i < j){
        while(i < j && arr[j] <= get)
            j--;
        arr[i] = arr[j];

        while(i < j && arr[i] >= get )
            i++;
        arr[j] = arr[i];
    }

    arr[i] = get;

    sort(arr,left , i-1);
    sort(arr,i + 1 ,right);
}

int main()
{
    int arr[] = {1,23,45,6,7,83,21,21};
    sort(arr,0,7);
    for(int i = 0;i < sizeof(arr)/sizeof(arr[0]);i++)
        printf("%d \t",arr[i]);
    return 0;
}

你可能感兴趣的:(C语言模拟实现标准库函数)