前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >09-排序1 排序 (25分)

09-排序1 排序 (25分)

作者头像
AI那点小事
发布2020-04-18 20:16:47
3560
发布2020-04-18 20:16:47
举报
文章被收录于专栏:AI那点小事AI那点小事

给定NN个(长整型范围内的)整数,要求输出从小到大排序后的结果。

本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:

数据1:只有1个元素; 数据2:11个不相同的整数,测试基本正确性; 数据3:103个随机整数; 数据4:104个随机整数; 数据5:105个随机整数; 数据6:105个顺序整数; 数据7:105个逆序整数; 数据8:105个基本有序的整数; 数据9:105个随机正整数,每个数字不超过1000。 输入格式:

输入第一行给出正整数NN(\le 10^5≤10 ​5 ​​ ),随后一行给出NN个(长整型范围内的)整数,其间以空格分隔。

输出格式:

在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。

输入样例:

11 4 981 10 -17 0 -20 29 50 8 43 -5 输出样例:

-20 -17 -5 0 4 8 10 29 43 50 981


AC代码:

代码语言:javascript
复制
#include <iostream>
using namespace std; 

int* copy(int* a,int len)
{
    int* num;
    num = new int[len];
    for ( int i = 0 ; i < len ; i++){
        num[i] = a[i];
    }
    return num;
}

void Buble_Sort(int *a ,int N)
{
    for ( int i = 0 ; i < N - 1 ; i++){
        for ( int j = i + 1 ; j < N ; j++){
            if ( a[i] > a[j]){
                a[i] = a[i] + a[j];
                a[j] = a[i] - a[j];
                a[i] = a[i] - a[j];
            }
        }
    }
}

void Insert_Sort(int *a ,int N)
{
    for ( int i = 1 ; i < N ; i++){
        int tmp = a[i];
        int j;
        for(j = i ; j > 0 && a[j-1] > tmp ; j--){
            a[j] = a[j-1];
        } 
        a[j] = tmp;
    }
}

/* 将N个元素的数组中以A[p]为根的子堆调整为最大堆 */
void PreDown(int *a , int p , int N)
{
    int parent,child;
    int x = a[p];
    for (parent = p ; (parent*2 +1) < N ; parent = child){
        child = 2 * parent+1;
        if ( (child != N-1)&& (a[child] < a[child+1])){
            child++;
        }
        if ( x >= a[child]){
            break;
        }else{
            a[parent] = a[child];
        }
    }
    a[parent] = x;
}

void Heap_Sort(int* a ,int N)
{
    int i;
    for ( i = N/2 - 1 ; i >= 0  ; i--){
        PreDown(a,i,N);
    }

    for ( i = N -1 ; i > 0 ; i--){
        a[0] = a[i] + a[0];
        a[i] = a[0] - a[i];
        a[0] = a[0] - a[i];
        PreDown(a,0,i);
    } 
}

void Merge1(int a[] ,int tmp[],int left ,int right,int rightend)
{
    int leftend = right-1;
    int len = rightend - left + 1;
    int x = left;
    while(left <= leftend && right <= rightend){
        if ( a[left] <= a[right]){
            tmp[x++] = a[left++];
        }else{
            tmp[x++] = a[right++];
        }
    }

    while(left <= leftend){
        tmp[x++] = a[left++];
    }
    while(right <= rightend){
        tmp[x++] = a[right++];
    }

    for( int i = rightend ; len > 0 ; len--,i--){
        a[i] = tmp[i];
    } 
}

void Mergesort(int a[],int tmp[],int left,int right)
{
    if ( left < right){
        int mid = (left+right)/2;
        Mergesort(a,tmp,left,mid);
        Mergesort(a,tmp,mid+1,right);
        Merge1(a,tmp,left,mid+1,right);
    }
}

void Merge_Sort1(int a[] ,int N)
{
    int* tmp;
    tmp = new int[N];
    if (tmp != NULL){
        Mergesort(a,tmp,0,N-1);
    }else{
        cout<<"内存不足"<<endl;
    } 
}

void swap(int* a ,int *b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int Median3(int a[],int left,int right)
{
    int center = (left + right) / 2;
    if (a[left] > a[center]) {
        swap(&a[left], &a[center]);
    }
    if (a[left] > a[right]) {
        swap(&a[left], &a[right]);
    }
    if (a[center] > a[right]) {
        swap(&a[center], &a[right]);
    }
    swap(&a[center], &a[right-1]); /* 将基准放在数组右端 */
    return a[right-1];
}

void Qsort(int a[],int left ,int right)
{
    int cutoff = 100;
    int low,high,pivot;
    if ( right - low > cutoff){
        pivot = Median3(a,left,right);
        low = left;
        high = right-1;
        while(1){
            while(a[++low] < pivot);
            while(a[--high] > pivot);
            if ( low < high){
                swap(&a[low],&a[high]);
            }else{
                break;
            }
        }
        swap(&a[low], &a[right - 1]);
        Qsort(a, left, low - 1);
        Qsort(a, low + 1, right);
    }else{
        Insert_Sort(a+left,right-left+1);
    }
}

void Quick_Sort(int a[],int N)
{
    Qsort(a,0,N-1);
 } 

void Print(int *a , int N)
{
    cout<<a[0];
    for ( int i = 1 ; i < N ; i++){
        cout<<" "<<a[i];
    }
}

int main()
{
    int len;
    cin>>len;
    int* a;
    a = new int[len];
    for ( int i = 0 ; i < len ; i++){
        cin>>a[i];
    } 

    //冒泡排序
    int* copy_num; 
    copy_num = copy(a,len);
    Buble_Sort(copy_num,len);
    Print(copy_num,len);
    cout<<endl;

    //插入排序
    copy_num = copy(a,len);
    Insert_Sort(copy_num,len);
    Print(copy_num,len);
    cout<<endl;

    //堆排序
    copy_num = copy(a,len);
    Heap_Sort(copy_num,len);
    Print(copy_num,len);
    cout<<endl;

    //归并排序(递归版) 
    copy_num = copy(a,len);
    Merge_Sort1(copy_num,len);
    Print(copy_num,len);
    cout<<endl;

    /*//快速排序
    copy_num = copy(a,len);
    Quick_Sort(copy_num,len);
    Print(copy_num,len);
    cout<<endl;*/ 

    return 0;
}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档