分治---线性时间选择

来自

点击打开链接

线性时间选择问题:给定线性序集中n个元素和一个整数k,1≤k≤n,要求找出这n个元素中第k小的元素,(这里给定的线性集是无序的)。

       1、随机划分线性选择

       线性时间选择随机划分法可以模仿随机化快速排序算法设计。基本思想是对输入数组进行递归划分,与快速排序不同的是,它只对划分出的子数组之一进行递归处理

程序解释:利用随机函数产生划分基准,将数组a[p:r]划分成两个子数组a[p:i]和a[i+1:r],使a[p:i]中的每个元素都不大于a[i+1:r]中的每个元素。接着"j=i-p+1"计算a[p:i]中元素个数j.如果k<=j,则a[p:r]中第k小元素在子数组a[p:i]中,如果k>j,则第k小元素在子数组a[i+1:r]中。注意:由于已知道子数组a[p:i]中的元素均小于要找的第k小元素,因此,要找的a[p:r]中第k小元素是a[i+1:r]中第k-j小元素。

      在最坏的情况下,例如:总是找到最小元素时,总是在最大元素处划分,这是时间复杂度为O(n^2)。但平均时间复杂度与n呈线性关系,为O(n)


#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
using namespace std;

void Swap(int &x,int &y)
{
    int temp = x;
    x = y;
    y = temp;
}

inline int Random(int x, int y)
{
     srand((unsigned)time(0));
     int ran_num = rand() % (y - x) + x;
     return ran_num;
}

int Partition(int r[],int s,int t)
{
    int x=r[s];//选r[s]为基准
    int i=s,j=t;
    while(i<j)
    {
        while(i<j&&r[j]>=x) j--;
        if(i<j) r[i]=r[j];
        while(i<j&&r[i]<=x) i++;
        if(i<j) r[j]=r[i];
    }
    r[i]=x;//基准放到正确位置
    for(int i=s;i<=t;i++) cout<<r[i]<<' ';
    cout<<endl;
    return i;//基准位置
}

int RandomizedPartion(int a[],int s,int t)
{
    int i=Random(s,t);
    Swap(a[i],a[s]);
    return Partition(a,s,t);
}

template <class Type>
Type RandomizedSelect(Type a[],int p,int r,int k)
{  //a[p:r]中找第k小元素
    if(p == r)
        return a[p];
    int i = RandomizedPartion(a,p,r);
    int j = i - p + 1;
    if(k <= j)
        return RandomizedSelect(a,p,i,k);
    else
    {
        //由于已知道子数组a[p:i]中的元素均小于要找的第k小元素
        //因此,要找的a[p:r]中第k小元素是a[i+1:r]中第k-j小元素。
        return RandomizedSelect(a,i+1,r,k-j);
    }
}

void RandomizedQuickSort(int r[],int first,int end)
{
    if(first<end)
    {
        int pivot=RandomizedPartion(r,first,end);
        RandomizedQuickSort(r,first,pivot-1);
        RandomizedQuickSort(r,pivot+1,end);
    }
}
int a[] = {49,38,65,97,76,13,27,49};
int main()
{
    for(int i=0; i<8; i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    cout<<RandomizedSelect(a,0,7,2)<<endl;
}

 中位数:是指将数据按大小顺序排列起来,形成一个数列,居于数列中间位置的那个数据。

      算法思路:如果能在线性时间内找到一个划分基准使得按这个基准所划分出的2个子数组的长度都至少为原数组长度的ε倍(0<ε<1),那么就可以在最坏情况下用O(n)时间完成选择任务。例如,当ε=9/10,算法递归调用所产生的子数组的长度至少缩短1/10。所以,在最坏情况下,算法所需的计算时间T(n)满足递推式T(n)<=T(9n/10)+O(n)。由此可得T(n)=O(n)。

     实现步骤

      (1)将所有的数n个以每5个划分为一组共组,将不足5个的那组忽略,然后用任意一种排序算法,因为只对5个数进行排序,所以任取一种排序法就可以了。将每组中的元素排好序再分别取每组的中位数,得到个中位数。

      (2)取这个中位数的中位数,如果是偶数,就找它的2个中位数中较大的一个作为划分基准。

      (3)将全部的数划分为两个部分,小于基准的在左边,大于等于基准的放右边。在这种情况下找出的基准x至少比个元素大。因为在每一组中有2个元素小于本组的中位数,有个小于基准,中位数处于,即个中位数中又有(n/5-1)/2=个小于基准x。因此至少有个元素小于基准x。同理基准x也至少比个元素小。而当n≥75时≥n/4所以按此基准划分所得的2个子数组的长度都至少缩短1/4。

      


#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
using namespace std;

template <class Type>
void Swap(Type &x,Type &y);

inline int Random(int x, int y);

void BubbleSort(int r[],int s,int t);

template <class Type>
int Partition(Type a[],int p,int r,Type x);

template <class Type>
Type Select(Type a[],int p,int r,int k);

int main()
{
    //初始化数组
    int a[100];
    //必须放在循环体外面
    srand((unsigned)time(0));
    for(int i=0; i<100; i++)
    {
        a[i] = Random(0,500);
        cout<<"a["<<i<<"]:"<<a[i]<<" ";
    }
    cout<<endl;

    cout<<"第83小元素是"<<Select(a,0,99,83)<<endl;

    //重新排序,对比结果
    BubbleSort(a,0,99);

    for(int i=0; i<100; i++)
        cout<<"a["<<i<<"]:"<<a[i]<<" ";
    cout<<endl;
}

template <class Type>
void Swap(Type &x,Type &y)
{
    Type temp = x;
    x = y;
    y = temp;
}

inline int Random(int x, int y)
{
     int ran_num = rand() % (y - x) + x;
     return ran_num;
}

void BubbleSort(int r[],int s,int t)
{//对r[s:t]排序
    int exchange=t;
    while(exchange)
    {
        int bound=exchange;//上次交换的位置
        exchange=0;
        for(int j=s;j<bound;j++)//只需比较到上次最后发生交换的位置
        {
            if(r[j]>r[j+1])
            {
                int temp=r[j];
                r[j]=r[j+1];
                r[j+1]=temp;
                exchange=j;//最终记录最后一次交换的位置
                //j是无序区最后一个数的位置
            }
        }
    }
}
template <class Type>
int Partition(Type s[],int l,int r,Type x)//快速排序中的一次划分
{
    int i = l - 1,j = r + 1;
    while(true)
    {
        while(s[++i]<x && i<r);
        while(s[--j]>x);
        if(i>=j)
        {
            break;
        }
        Swap(s[i],s[j]);
    }
    return j;
}
template <class Type>
Type Select(Type a[],int p,int r,int k)
{
    if(r-p<75)
    {
        BubbleSort(a,p,r);
        return a[p+k-1];
    }
    //r-p-4相当于n-5
    for(int i=0; i<=(r-p-4)/5; i++)
    {
        //将元素每5个分成一组,分别排序,并将该组中位数与a[p+i]交换位置
        //使所有中位数都排列在数组最左侧,以便进一步查找中位数的中位数
        BubbleSort(a,p+5*i,p+5*i+4);//将每一组的5个元素排序
        cout<<"第"<<i<<"组中位数:"<<a[p+5*i+2]<<endl;
        Swap(a[p+5*i+2],a[p+i]);
    }
    //找中位数的中位数
    Type x = Select(a,p,p+(r-p-4)/5,(r-p-4)/10);
    cout<<"中位数的中位数x="<<x<<endl;
    int i = Partition(a,p,r,x);
    int j = i-p+1;
    if(k<=j)
        return Select(a,p,i,k);
    else
        return Select(a,i+1,r,k-j);
}


代码中的中位数的中位数x并非真正意义上的中位数:

若中位数有奇数个,即共分奇数个组,代码中x是比中位数小的那个数;若中位数有偶数个,即共分偶数个组,代码中的x是比中间两个数小的那个数。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值