首页 > 编程语言 > 详细

6、c++版比较牛的各种排序

时间:2016-08-30 22:36:21      阅读:217      评论:0      收藏:0      [点我收藏+]
#include<iostream>
using namespace std;
//-------直接插入排序
void InsertSort(ElemType A[],int n)
{
    ElemType x;
    int i,j;
    for(i=1;i<n;i++)
    {
        x=A[i];
        for(j=i-1;j>=0;j--)
            if(x.stn<A[j].stn) A[j+1]=A[j];
            else break;
        A[j+1]=x;
    }
}
//----------------------
//-------希尔排序-----
void ShellSort(ElemType A[],int n)
{
    ElemType x;
    int i,j,d;
    for(d=n/2;d>=1;d/=2)
    {
        for(i=d;i<n;i++)
        {
            x=A[i];
            for(j=i-d;j>=0;j-=d)
            {
                if(x.stn<A[j].stn) A[j+d]=A[j];
                else break;
            }
            A[j+d]=x;
        }
    }
}
///-----直接选择排序--
void SelectSort(ElemType A[],int n)
{
    ElemType x;
    int i,j,k;
    for(i=1;i<=n-1;i++)
    {
        k=i-1;
        for(j=i;j<=n-1;j++)
        {
            if(A[j].stn<A[k].stn) k=j;
        }
        if(k!=i-1)
        {
            x=A[i-1];A[i-1]=A[k];A[k]=x;
        }
    }
}
//---------冒泡排序----------
void BubbleSort(ElemType A[],int n)
{
    ElemType x;
    int i,j,flag;
    for(i=1;i<=n-1;i++)
    {
        flag=0;
        for(j=n-1;j>=i;j--)
            if(A[j].stn<A[j-1].stn)
            {
                x=A[j-1];A[j-1]=A[j];A[j]=x;
                flag=1;
            }
        if(flag==0) return;
    }
}
int main()
{
    
    return 0;
    }

 

6、c++版比较牛的各种排序

原文:http://www.cnblogs.com/weizhen/p/5823885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!