排序算法实现

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<cstdio>
#include<string.h>
using namespace std;
int temp;//哨兵
int len=10000;//对十万个数排序,有重复
bool isPrint=false;//是否输出数组
vector<int> myArray;

double startT,endT;//计算时间

void print(vector<int> myVector)
{
    for(int i=0;i<len;i++)
    {
        cout<<myVector[i]<<" ";
        if(i%10==0&&i!=0)
            cout<<endl;
    }
    cout<<endl;
}
//初始化函数排序数范围在0~99999,有重复,总数100000
void init(){
    srand((unsigned)time(NULL));
    for(int i=0;i<len;i++)
    {
        myArray.push_back(rand()%len);
    }
}

void swap(int &a,int &b)
{
    int temp=a;
    a=b;
    b=temp;
}

//快排子函数
//快速排序选取第一个数为标兵,返回最后在数组中位置
int Partition(vector<int> &tempArray,int low,int high){
    int temp=tempArray[low];
    while(low<high)
    {
        while(high>low&&tempArray[high]>=temp)
        {
            high--;
        }
        swap(tempArray[high],tempArray[low]);
        while(low<high&&tempArray[low]<=temp)
        {
            low++;
        }
        swap(tempArray[high],tempArray[low]);
    }
    tempArray[low]=temp;
    return low;
}
//快速排序
void QuickSort(vector<int> &tempArray,int low,int high){
    if(low<high)
    {
        int pivot=Partition(tempArray,low,high);
        QuickSort(tempArray,low,pivot);
        QuickSort(tempArray,pivot+1,high);
    }
}

//计数排序
vector<int> CountingSort(){
    int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
    int max=myArray[pos]+1;
    vector<int> count(max);//计数数组
    vector<int> result(len);//排序结束数组
    for(int i=0;i<len;i++)
    {
        count[myArray[i]]++;
    }
    for(int i=1;i<=max;i++)
    {
        count[i]=count[i]+count[i-1];
    }
    for(int i=0;i<len;i++)
    {
        count[myArray[i]]--;
        result[count[myArray[i]]]=myArray[i];
    }
    return result;
}

//桶排序
//最快情况是取每个桶大小为1,但这个意义不大,几乎等同于计数排序了,因此我们取桶大小为100,桶内部用sort(一般为改进版快排)
int sizeOfBucket=10000;
vector<int> BucketSort(){
    int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
    int max=myArray[pos]+1;

    vector<vector<int>> bucket(max/sizeOfBucket+1);
    for(int i=0;i<len;i++)
    {
        bucket[myArray[i]/sizeOfBucket].push_back(myArray[i]);
    }
    for(int i=0;i<max/sizeOfBucket+1;i++)
    {
        sort(bucket[i].begin(),bucket[i].end());
    }
    vector<int> result;
    for(int i=0;i<max/sizeOfBucket+1;i++)
    {
        for(int j=0;j<bucket[i].size();j++)
        {
            result.push_back(bucket[i][j]);
        }
    }
    return result;
}

//基数排序子函数(用桶排序)
void ExpSort(vector<int>&tempArray,int exp){
    int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
    int max=myArray[pos]+1;
    vector<vector<int>> bucket(max/exp+1);
    for(int i=0;i<len;i++)
    {
        bucket[myArray[i]/exp].push_back(myArray[i]);
    }
    for(int i=0;i<max/exp+1;i++)
    {
        sort(bucket[i].begin(),bucket[i].end());
    }
    vector<int> result;
    for(int i=0;i<max/exp+1;i++)
    {
        for(int j=0;j<bucket[i].size();j++)
        {
            result.push_back(bucket[i][j]);
        }
    }
    tempArray=result;
}

//基数排序 分别对个位,十位等排序
vector<int> RadixSort(vector<int> tempArray){
    int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
    int max=myArray[pos]+1;
    for(int exp=1;max/exp>0;exp*=10)
    {
        ExpSort(tempArray,exp);
    }
    return tempArray;
}

int main() {
    init();
    vector<int> tempArray(myArray);
    if(isPrint) {
        cout<<"初始数组:"<<endl;
        print(myArray);
    }

    cout<<"sort排序: ";
    startT=clock();
    vector<int> sortArray(tempArray);
    sort(sortArray.begin(),sortArray.end());
    endT=clock();
    cout<<"时间 "<<endT-startT<<endl;
    if(isPrint)
        print(tempArray);


    cout<<"计数排序: ";
    startT=clock();
    tempArray=CountingSort();
    endT=clock();
    cout<<"时间 "<<endT-startT<<endl;
    if(isPrint)
        print(tempArray);


    cout<<"桶排序:";
    startT=clock();
    tempArray=BucketSort();
    endT=clock();
    cout<<"时间 "<<endT-startT<<endl;
    if(isPrint) {
        print(tempArray);
    }

    cout<<"基数排序:";
    startT=clock();
    tempArray=RadixSort(tempArray);
    endT=clock();
    cout<<"时间 "<<endT-startT<<endl;
    if(isPrint) {
        print(tempArray);
    }

    cout<<"快速排序:";
    startT=clock();
    QuickSort(tempArray,0,len-1);
    endT=clock();
    cout<<"时间 "<<endT-startT<<endl;
    if(isPrint)
        print(tempArray);

    return 0;
}

 

1.冒泡排序原理:通过相邻的两个数据之间的比较和交换,使关键码较小的记录逐渐从底部上浮,关键码较大的记录逐渐从顶部下沉。 算法实现:对n个数据进行n-1次排序,每次从剩余元素的第一个开始进行相邻元素交换比较,实现由小到大的排序。 2.选择排序原理:通过多次关键码的比较,使得在每一趟排序中第一位最小。 算法实现:对n个数据,比较n-1趟,在每趟区间中将最小数下标记录在k中,若k不为1,将b[1]与b[k]交换,始终保持剩余元素的第一个数为该趟最小值,实现由小到大的排序。 3.插入排序原理:将原序列逐个分开,每次比较插入一个新的关键码,与已经排好序的记录码比较,寻找合适位置插入该记录码,实现排序。 算法实现:对n个数据,比较n-1趟,以第一个数据为初始序列,每趟插入一个原序列中的关键码,并进行比较,若找到序列中比该关键码大的数据,则该数据之前一位即为插入位置,将该数据连同之后的所有数据后移一位,进行插入,实现由小到大的排序。 4.快速排序原理:通过一趟排序将要排序的记录分割成独立的两部分,其中一部分的所有记录关键码比另一部分的都小,再按此方法对两部分数据进行递归,实现快速排序。 算法实现:从每趟数据的左边界向右搜索一个比它大的数据1,同时从右边界搜索一个比它小的数据2,若数据1的下标大于数据2的下标,则交换位置,如此循环,再对关键数据的左半部分和右半部分进行递归,实现由小到大的排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值