用快排解決Leetcode 169. Majority Element(c++實現)

快速排序

快速排序的基本思想:通過一趟排序將待排記錄分隔成獨立的兩部分,其中一部分記錄的關鍵字均比另一部分的關鍵字小,則可分別對這兩部分記錄繼續進行排序,以達到整個序列有序。

算法描述
快速排序使用分治法來把一個串(list)分爲兩個子串(sub-lists)。具體算法描述如下:

  1. 從數列中挑出一個元素,稱爲 “基準”(pivot);
  2. 重新排序數列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的後面(相同的數可以到任一邊)。在這個分區退出之後,該基準就處於數列的中間位置。這個稱爲分區(partition)操作;
  3. 遞歸地(recursive)把小於基準值元素的子數列和大於基準值元素的子數列排序。

複雜度:
平均時間複雜度: O(nlog2n)O(nlog_2n)
最壞時間複雜度:O(n2)O(n^2)
最好時間複雜度:O(nlog2n)O(nlog_2n)
空間複雜度:O(nlog2n)O(nlog_2n)
c++代碼:

#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int& a, int& b)
{
        int temp = a;
        a= b;
        b = temp;
}
int Partition(int* a ,int start, int end)
{
        int r = rand()%(end-start+1)+start;
        swap(a[r], a[end]);
        int small = start-1;
        for(int index= start; index<end; index++)
        {
                if(a[index]<a[end])
                {
                        small++;
                        if(small!=index)
                                swap(a[small], a[index]);
                }
        }
        small++;
        swap(a[small], a[end]);
        return small;
}
void quicksort(int* a, int start, int end)
{
        if(start==end)
                return;
        int index = Partition(a, start, end);
        if(index>start)
                quicksort(a, start, index-1);
        if(index<end)
                quicksort(a, index+1, end);

}
int main()
{
        int a[8]={6,8,4,6,1,8,3,0};
        quicksort(a,0,7);
        for(int i=0;i<8;i++)
                cout<<a[i]<<" ";
        cout<<endl;
}

leetcode 169

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3
Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

快排過程中直到劃分點的index爲middle時,此時該中間結點值一定是出現次數大於⌊ n/2 ⌋ 的元素.

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int length = nums.size();
        int middle = length>>1;
        int start = 0;
        int end = length-1;
        int index = partition(nums, start, end);
        while(index!=middle)
        {
            if(index<middle)
                index = partition(nums, index+1, end);
            else if(index>middle)
                index = partition(nums, start, index-1);
        }
        return nums[middle];
        
    }
    int partition(vector<int>& nums, int start, int end)
    {
        int r = rand()%(end-start+1)+start;
        swap(nums[r], nums[end]);
        int small=start-1;
        for(int index=start; index<end; index++)
        {
            if(nums[index]<nums[end])
            {
                small++;
                if(index!=small)
                {
                    swap(nums[index], nums[small]);
                }
            }
        }
        small++;
        swap(nums[small], nums[end]);
        return small;
    }
    void swap(int &a, int &b)
    {
        int temp=a;
        a=b;
        b=temp;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章