用快排解决Leetcode 169. Majority Element(c++实现)

快速排序

快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。

算法描述
快速排序使用分治法来把一个串(list)分为两个子串(sub-lists)。具体算法描述如下:

  1. 从数列中挑出一个元素,称为 “基准”(pivot);
  2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;
  3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

复杂度:
平均时间复杂度: O ( n l o g 2 n ) O(nlog_2n) O(nlog2n)
最坏时间复杂度: O ( n 2 ) O(n^2) O(n2)
最好时间复杂度: O ( n l o g 2 n ) O(nlog_2n) O(nlog2n)
空间复杂度: O ( n l o g 2 n ) O(nlog_2n) O(nlog2n)
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;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值