我的快排

很遗憾, 超时了. 测试用例都过了

https://leetcode-cn.com/problems/sort-an-array/

 

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 // 9 10 8 6 3 8 1
 // 9 1  8 6 3 8 10
 // 9 1  8 6 3 8 10
// 3 1 2  4 5

// [left, right)
int* quickSort(int* nums, int left, int right) {
    if (right - left <= 1) {
        return nums;
    }
    int tmp = nums[left];
    int i = left; int j = right - 1;
    while(i != j) {
        while(nums[j] >= tmp && i != j) {
            j--;
        }
        if (i == j) {
            break;
        }
        nums[i++] = nums[j];

        while(nums[i] < tmp && i != j) {
            i++;
        }
        if (i == j) {
            break;
        }
        nums[j--] = nums[i];
    }

    nums[i] = tmp;
    if (i - left > 1)
        quickSort(nums, left, i);
    if (right - i - 1 > 1) 
        quickSort(nums, i + 1, right);

    return nums;
}

int* sortArray(int* nums, int numsSize, int* returnSize){
    printf("numsSize=%d", numsSize);
    quickSort(nums, 0, numsSize);
    *returnSize = numsSize;
    return nums;
}

  

posted @ 2022-03-25 14:39  yushimeng  阅读(15)  评论(0编辑  收藏  举报