多种排序算法的比较

使用PAT上的题目测试不同排序算法,进行比较

07-排序1. 排序(25)

http://www.patest.cn/contests/mooc-ds/07-%E6%8E%92%E5%BA%8F1

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

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

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

输入格式:

输入第一行给出正整数N(<= 105),随后一行给出N个(长整型范围内的)整数,其间以空格分隔。

输出格式:

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

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

要求是:

时间限制
5000 ms
内存限制
128000 kB
代码长度限制
8000 B
 
Method1:冒泡排序
#include <string>
#include <iostream>
using namespace std;

typedef int ElementType;

void Swap(ElementType *a,ElementType *b)
{
    ElementType swaper;
    swaper = *a;
    *a = *b;
    *b =swaper;
}

void Bubble_sort(ElementType A[],long N)
{
    long i,j;
    int isFinished=0;
    for(i=0;i<N;i++)
    {
        isFinished=1;
        for(j=N-1;j>i;j--)
        {
            if(A[j]<A[j-1])
            {
                Swap(&A[j],&A[j-1]);
                isFinished=0;
            }
        }
        if(1 == isFinished)
            break;
    
    }
}


int main()
{
    ElementType *pData;
    long lLength;

    cin>>lLength;

    pData =(ElementType *)malloc(sizeof(ElementType)*lLength);
    
    int iCounter;
    int iInput;

    iCounter=0;
    while(iCounter<lLength)
    {
        cin>>iInput;
        *(pData+iCounter)=iInput;
        iCounter++;
    }

    Bubble_sort(pData,lLength);

    iCounter=0;
    int isFirstOutput=1;
    while(iCounter<lLength)
    {
        if(1 == isFirstOutput)
        {
            cout<<*(pData+iCounter);
            isFirstOutput=0;
        }
        else
        {
            cout<<" "<<*(pData+iCounter);
        }
        iCounter++;
    }

    system("pause");
    return 0;
}

运行结果:

Method2:插入排序

void Insert_Sort(ElementType A[],int N)
{
    int i;
    int P;
    ElementType Temp;
    for(P=1;P<N;P++)
    {
        Temp = A[P];
        for(i=P;(i>0)&&(A[i-1] > Temp);i--)
        {
            A[i] = A[i-1];
        }
        A[i] = Temp;
    }


}

运行结果

希尔排序

void Shell_Sort(ElementType A[],int N)
{
    int D;
    int P;
    ElementType Temp;
    for(D=N/2;D>0;D/=2)
    {
        for(P=D;P<N;P++)
        {
            Temp = A[P];
            int i;
            for(i=P;i>=D && A[i-D]>Temp;i-=D)
            {
                A[i] = A[i-D];
            }
            A[i] = Temp;
        
        }
    }

}

运行结果

堆排序

///////////Heap_Sort////////////
void PercDown(ElementType A[],int i,int N)
{
    int Parent,Child;
    ElementType Temp = A[i];

    for(Parent = i;(2*Parent+1)<=(N-1);Parent = Child )
    {
        Child = 2*Parent+1;
        if( (Child<(N-1)) && (A[Child]<A[Child+1]) )
            Child++;
        if(Temp > A[Child])
            break;
        else
            A[Parent] = A[Child];
    }
    A[Parent] = Temp;

}

void Heap_Sort(ElementType A[],int N)
{//This is a HeadForMax heap
    int i;
    for(i=N/2;i>=0;i--)
        PercDown(A,i,N);
    for(i=N-1;i>0;i--)
    {
        Swap(&A[0],&A[i]);//Swap A[0] and A[i],then the max number is A[i]
        PercDown(A,0,i);
    
    }


}

运行结果

posted on 2015-04-22 22:40  依风152  阅读(140)  评论(0)    收藏  举报