• 各种排序算法汇总


    #include<iostream>
    #include
    <cstdio>
    #include
    <algorithm>
    #include
    <cmath>
    using namespace std;

    template
    <class T>
    void InsertSort(T a[],int n)//直接插入排序,时间复杂度为O(n^2)
    {

        
    int i,j;
        T temp;
        
    for(i=1;i<n;i++)
        
    {

            temp
    =a[i];
            
    for(j=i-1;j>=0;j--)
            
    {

                
    if(temp<a[j])
                    a[j
    +1]=a[j];
                
    else
                    
    break;
            }

            a[j
    +1]=temp;
            
        }

    }




    template
    <class T>
    void ShellSort(T a[],int n)//希尔排序,时间复杂度为O(n^1.5)
    //ShellSort



    template
    <class T>
    void BubbleSort(T a[],int n)//冒泡排序,时间复杂度为O(n^2)



    /**/
    template<class T>
    int Partion(T a[],int i,int j)//划分函数



    template 
    <class T>
    void qsort(T a[],int l,int h)
    {
        
    int m;
        
    if(l<h) 
        

            m
    =Partion(a,l,h);
            qsort(a,l,m
    -1);
            qsort(a,m
    +1,h); 
        }

    }


    template
    <class T>
    void QuickSort(T a[],int n)
    {
        qsort(a,
    0,n-1);
    }


    ////////////////////QuickSort_O(nlog2n)////////////////////////



    template 
    <class T>
    void SelectSort(T a[],int n)//选择排序,时间复杂度为O(n^2)
    {
        
    int i,j,k;
        T temp;
        
    for(i=0;i<n-1;i++)
        
    {   
            k
    =i;
            
    for(j=i+1;j<n;j++)
            
    {
                
    if(a[j]<a[k])
                    k
    =j;
            }

                temp
    =a[k];
                a[k]
    =a[i];
                a[i]
    =temp;
        }

    }


    ///////////////////////堆排序////////////////////////////
    template <class T>
    void Sift(T a[],int k,int m)//k筛选标号,m筛选范围
    {
        
    int i,j;
        T temp;
        i
    =k;
        j
    =2*i+1;//j指向左儿子;
        temp=a[i];//暂存a[i];
        while(j<=m)
        

            
    if(j<&&a[j]<a[j+1])
                    j
    ++;
            
    if(temp<a[j])
            

                a[i]
    =a[j];
                i
    =j;
                (j
    <<=1)++;//j*2+1
            }

            
    else 
                
    break;
        }

        a[i]
    =temp; 
    }
            


    template 
    <class T>
    void HeapSort(T a[],int n)//堆排序,建堆时间复杂度O(n),筛选O(nlog2n);

    {
        
    int i;
        T temp;
        
    for(i=(n>>1)-1;i>=0;i--)
            Sift(a,i,n
    -1);

        
    for(i=n-1;i>=1;i--)
        
    {  
            temp
    =a[0];
            a[
    0]=a[i];
            a[i]
    =temp;
            Sift(a,
    0,i-1);
        }

    }

    /////////////////////HeapSort_O(nlog2n)///////////////////////////




    ///////////////////////归并排序,时间复杂度O(nlog2n)/////////////////////////////


    template 
    <class T>
    void Merge(T sr[],T tr[],int l,int m,int n)
    {
        
    int i,j,k;
        i
    =l;
        j
    =m+1;
        k
    =l-1;
        
    while(i<=&& j<=n)
        
    {
            
    if(sr[i]<sr[j]) 
                tr[
    ++k]=sr[i++];
            
    else 
                tr[
    ++k]=sr[j++];
        }

            
    while(i<=m)
                tr[
    ++k]=sr[i++];
            
    while(j<=n)
                tr[
    ++k]=sr[j++];
            
    for(i=l;i<=n;i++
                sr[i]
    =tr[i];
    }


    template 
    <class T>
    void Msort(T a[],T st[],int s,int t)
    {
        
    int m;
        
    if(s<t) 
        

            m
    =(s+t)>>1;
            Msort(a,st,s,m);
            Msort(a,st,m
    +1,t);
            Merge(a,st,s,m,t);
        }

    }


    template 
    <class T>
    void MergeSort(T a[],int n)

        T 
    *st=new T[n];
        Msort(a,st,
    0,n-1);  
        delete  [ ]st;
    }

    //////////////////////MergeSort_O(nlog2n)///////////////////////////////


    /////////////////////////END_TEMPLATE_BY_ABILITYTAO_ACM//////////////////////////////////////






    int main ()
    {
        
    double test1[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        InsertSort(test1,
    sizeof(test1)/sizeof(double));
        
    double test2[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        ShellSort(test2,
    sizeof(test2)/sizeof(double));
        
    double test3[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        BubbleSort(test3,
    sizeof(test3)/sizeof(double));
        
    double test4[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        QuickSort(test4,
    sizeof(test4)/sizeof(double));
        
    double test5[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        SelectSort(test5,
    sizeof(test5)/sizeof(double));
        
    double test6[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        HeapSort(test6,
    sizeof(test6)/sizeof(double));
        
    double test7[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
        MergeSort(test7,
    sizeof(test7)/sizeof(double));
        
    return 0;
    }








    写这些函数的过程中我遇到了一个奇怪的问题,就是merge这个函数前不能使用上模板类,如果将它改为:
    template <class T>
    void merge(T sr[],T tr[],int l,int m,int n)
    编译器竟然会报错,不知道是怎么回事,希望各位大牛指点一下。

    呵呵 问题已经解决,原来在C++标准库里面已经使用过merge这个关键字,只要将merge改为Merge便可通过编译了。

    来源:http://www.cppblog.com/abilitytao/archive/2009/05/06/82075.html

  • 相关阅读:
    Linux 自定义快捷命令
    Linux 用户登陆提示This account is currently not available
    Linux 切换用户提示Permission denied
    Netty核心组件之ChannelPipeline
    Netty核心组件之ChannelHandler
    Netty核心组件之Channel
    Netty核心组件之ChannlFuture
    Error creating bean with name 'eurekaAutoServiceRegistration'
    关于mysql数据库时间与java后台时间类型
    rabbitMQ-helloWorld
  • 原文地址:https://www.cnblogs.com/ganmk/p/1459580.html
Copyright © 2020-2023  润新知