用快排解决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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章