算法-快速排序

可参考https://www.cnblogs.com/sdlwlxf/p/5131793.html

0、一趟快排 单向 最简洁

int partition(int a[], int p, int r)
{
    int x = a[r];
    int i = p;
    int j = p;
    for (; j < r; ++j)
        if (a[j] < x) {
            if (i != j)
                swap(&a[i], &a[j]);
            i++;
        }
    swap(&a[i], &a[j]);
    return i;
}

1、一趟随机快速排序--双向扫描

/***************
 * 一趟随机快速排序
 * 输入 数组 长度 坐标起始 坐标终止
 * 输出 快排结束后随机关键字的下标
 **************/
int Partition(int data[], int length, int start, int end){
    //获得[start,end]之间的随机位置
    int random = rand()%(end - start + 1) + start;
    cout<<"random:"<<random<<endl;
    swap(data[0],data[random]);
    int key = data[0];
    while(start<end){
        while(start<end&&data[end]>=key)
            end--;
        data[start]=data[end];
        while(start<end&&data[start]<=key)
            start++;
        data[end]=data[start];
    }   
    data[start]=key;
    for(int i=0;i<length;i++)
        cout<<data[i]<<' ';
    cout<<endl;
    return start;
}

2.一趟随机快速排序——单向扫描

/******
 *减少循环次数的一趟 随机 快速排序
 *****/
int Partition(int data[], int length, int start, int end)
{
    if(data == nullptr || length <= 0 || start < 0 || end >= length)
        throw new std::exception("Invalid Parameters");

    int index = rand()%(end - start + 1) + start;
    Swap(&data[index], &data[end]);

    int small = start - 1;
    for(index = start; index < end; ++ index)
    {
        if(data[index] < data[end])
        {
            ++ small;
            if(small != index)
                Swap(&data[index], &data[small]);
        }
    }

    ++ small;
    Swap(&data[small], &data[end]);

    return small;
}

3.第一种方式的递归快速排序

/***************
 *递归快速排序
 **************/
void Partition_all(int data[], int length, int start, int end){
    //获得[start,end]之间的随机位置
    //int random = rand()%(end - start + 1) + start;
    //cout<<"all--andom:"<<random<<endl;
    //swap(data[0],data[random]);
    if(start>=end)
        return;
    int t_start = start;
    int t_end = end;
    int key = data[start];
    while(start<end){
        while(start<end&&data[end]>=key)
            end--;
        data[start]=data[end];
        while(start<end&&data[start]<=key)
            start++;
        data[end]=data[start];
    }
    data[start]=key;
    /*
    for(int i=0;i<length;i++)
        cout<<data[i]<<' ';
    cout<<endl;
    */
    Partition_all(data,length,t_start,start-1);   //注意递归范围 不包括已经排序完成的那个基准值元素
    Partition_all(data,length,start+1,t_end);
}

该数据集涵盖2000年至2023年中国A股上市公司机构投资者的持股特征,重点区分长期与短期机构投资者的持股比例。数据覆盖5362家上市公司,包含5.9万个样本,提供原始数据、计算代码及结果验证文件。数据构建方法参考《会计研究》周绍妮(2017)的机构投资者异质性研究,通过计算机构投资者的平均换手率(AVG_CR)划分投资期限:将换手率由低到高排序后分为三组,最低组定义为长期投资者(稳定型),最高组为短期投资者(交易型),并分别计算两类投资者在每只股票中的持股比例(持股数量占当期流通股总数的比例)[citation:1]。数据字段包括证券代码、年份、总股数、流通股份、短期与长期机构投资者持股数量及比例等核心指标。例如,某上市公司2007年流通股份为175.68亿股,短期机构持股22.87亿股(占比13.02%),长期机构持股25.46亿股(占比14.49%),数据颗粒度细化至单一年份与单只股票层级[citation:1]。此外,数据还包含异常值处理逻辑,例如剔除持股比例缺失或超过100%的异常记录,并对连续变量进行上下1%的Winsorize缩尾处理,确保数据质量[citation:1]。该数据集适用于研究机构投资者异质性对公司治理、并购绩效、盈余管理等领域的影响,也可用于分析市场稳定性与投资者行为关联性。配套文献包含机构投资者分类方法、投资期限与市场调节作用的经典研究,为学术验证提供理论支撑[citation:1][citation:7]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值