美文网首页
求数组中第N大的数

求数组中第N大的数

作者: LEO_青蛙 | 来源:发表于2020-05-30 14:31 被阅读0次

方法1:选择、插入、冒泡排序
时间复杂度:O(n^2)
方法2:堆排序、快速排序
时间复杂度:O(n*logn)
方法3:快速排序+剪枝
时间复杂度:O(n)

public int QuickSort(int[] arr, int left, int right, int n)
{
    if(left >= right)
        return;
    int low = left;
    int high = right;
    int value = arr[low];
    while(low < high)
    {
        while(low<high && arr[high]>=value)
            high--;
        arr[low] = arr[high];
        while(low<high && arr[low]<=value)
            low++;
        arr[high] = arr[low];
    }
    arr[low] = value;
    if(low == n)
        return arr[low];
    if(low < n)
        return QuickSort(arr, low+1, right, n);
    else
        return QuickSort(arr, left, low-1, n);
}

相关文章

网友评论

      本文标题:求数组中第N大的数

      本文链接:https://www.haomeiwen.com/subject/xhgczhtx.html