經典演算法之快速選擇演算法

來源:互聯網
上載者:User

標籤:swa   思路   ==   不同的   void   快速   出錯   pivot   class   

相信快速排序演算法這種經典的演算法大家並不陌生。但是基於快速演算法的各種變形,你瞭解嗎?

 

其中很重要的一種變形就是快速選擇演算法, 通常用來在未排序的數組中尋找第k小/第k大的元素。快速選擇及其變種是實際應用中最常使用的高效選擇演算法。

 

快速選擇的總體思路與快速排序一致,選擇一個元素作為基準來對元素進行分區,將小於和大於基準的元素分在基準左邊和右邊的兩個地區。不同的是,快速選擇並不遞迴訪問雙邊,而是只遞迴進入一邊的元素中繼續尋找。這降低了平均時間複雜度,從O(n log n)至O(n),不過最壞情況仍然是O(n2)。

 

lc上一道題:

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

 

下面的解法注釋詳細,能很好的理解快速選擇演算法,這個可以當作模版記下來。面試時能一次ac不出錯嗎?其實不是那麼容易。記住的關鍵在於深刻的理解。

 

class Solution {    /** *  解法0. 堆 O(klogn) *  解法1. 快速選擇: O(n) */    public int findKthLargest(int[] nums, int k) {        if (nums.length == 0 || nums == null) return 0;        int left = 0, right = nums.length - 1;        while (true) {            int position = partition(nums, left, right);            if (position == k - 1) return nums[position]; //每一輪返回當前pivot的最終位置,它的位置就是第幾大的,如果剛好是第K大的數            else if (position > k - 1) right = position - 1; //二分的思想            else left = position + 1;        }    }    private int partition(int[] nums, int left, int right) {        int pivot = left;        int l = left + 1; //記住這裡l是left + 1        int r = right;        while (l <= r) {            while (l <= r && nums[l] >= nums[pivot]) l++; //從左邊找到第一個小於nums[pivot]的數            while (l <= r && nums[r] <= nums[pivot]) r--; //從右邊找到第一個大於nums[pivot]的數            if (l <= r && nums[l] < nums[pivot] && nums[r] > nums[pivot]) {                swap(nums, l++, r--);            }        }        swap(nums, pivot, r); //交換pivot到它所屬的最終位置,也就是在r的位置,因為此時r的左邊都比r大,右邊都比r小        return r; //返回最終pivot的位置    }    private void swap(int[] nums, int l, int r) {        int tmp = nums[l];        nums[l] = nums[r];        nums[r] = tmp;    }    }

 

經典演算法之快速選擇演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.