各种排序算法总结(待完成)

参考:

http://blog.csdn.net/lambp314/archive/2009/02/25/3932270.aspx

 

http://blog.csdn.net/StuRun/archive/2008/12/11/3491225.aspx(简洁,好)

 

http://blog.csdn.net/yuguanglou/archive/2004/11/24/193133.aspx(类模板)

 

http://blog.csdn.net/hlf48641715/archive/2009/05/11/4166054.aspx

 

http://blog.csdn.net/Learner9023/archive/2009/12/26/5077600.aspx

 

http://blog.csdn.net/ssfp8762/archive/2009/12/18/5035046.aspx 

 

 

 

//参考算法I-IV基础、数据结构、排序和搜索
//张铭泽 译 TP301.6 S014A2

 

#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;

//生成随机数的函数
void randomNum(int N,int p[])
{
 srand((unsigned long)time(NULL));
 for(int i=0;i<N;i++)
 {
  p[i] = rand();
 }
}
template <class Item>
void Print(Item p[],int N)
{
 cout<<"排序结果是:"<<endl;
 for(int i=0;i<N;i++)
  cout<<p[i]<<endl;
}

template <class Item>
void exch(Item& A,Item& B)
{
 Item temp = A;
 A = B;
 B = temp;
}

template <class Item>
void compexch(Item& A,Item& B)
{
 if(B<A)
  exch(A,B);
}

//插入排序,对数组第l和r间(含a[l]和a[r])的元素进行排列
template <class Item>
void sort(Item a[],int l,int r)
{
 for(int i=l+1;i<=r;i++)
  for(int j=i;j>l;j --)
   compexch(a[j-1],a[j]);
}

//选择排序
template <class Item>
void Selection(Item a[],int l,int r)
{
 for(int i=l;i<r;i++)
 {
  int min = i;
  for(int j=i+1;j<=r;j++)
   if(a[j]<a[min])
    min = j;
  exch(a[i],a[min]);
 }
}

//插入排序
template <class Item>
void Insertion(Item a[],int l,int r)
{
 for(int i=r;i>l;i--)
  compexch(a[i-1],a[i]);
 for(i=l+2;i<=r;i++)
 {
  int j=i;
  Item v = a[i];
  while(v<a[j-1])
  {
   a[j] = a[j-1];
   j--;
  }
  a[j] = v;
 }
}

//冒泡排序
template <class Item>
void Bubble(Item a[],int l,int r)
{
 for(int i=l;i<r;i++)
  for(int j=r;j>i;j--)
   compexch(a[j-1],a[j]);
}

//Shell排序
template <class Item>
void Shell(Item a[],int b,int r)
{
 for(int h=1;h<=(r-1)/9;h=3*h+1);
 for(;h>0;h/=3)
  for(int i=b+h;i<=r;i++)
  {
   int j = i;
   Item v = a[i];
   while(j>=b+h && v<a[j-h])
   {
    a[j] = a[j-h];
    j-=h;
   }
   a[j] = v;
  }
}

int main()
{
 int N;
 cout<<"请输入要排序的数目:";
 cin>>N;
 int *p = new int[N];
 randomNum(N,p);
// sort(p,0,N-1);
// Selection(p,0,N-1);  //选择排序
// Insertion(p,0,N-1);  //插入排序
// Bubble(p,0,N-1);
 Shell(p,0,N-1);
 Print(p,N);
 delete []p;
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值