各种排序算法的实现:选择排序、插入排序、冒泡排序、堆排序、归并排序、基数排序

#include <iostream>
using namespace std;
template<class ElemType>
void insertSort(ElemType array[],int n)
{
int i,j;
for(i=1;i<n;i++)
{
  ElemType key = array[i];
  for(j=i-1;j>=0;j--)
    {
    if(array[j]>key){array[j+1]=array[j];}
        else {break;}
    }
  array[j+1]=key;
}
}
template<class ElemType>
void swap(ElemType *a,ElemType *b)
{
ElemType tmp;
tmp=*a;*a=*b;*b=tmp;
cout<<"swap "<<*a<<' '<<*b<<endl;
}
template<class ElemType>
void bubleSort(ElemType array[],int n)
{
int i,j;
bool flag=false;
for(i=n-1;i>=1;i--)
{
flag=false;
for(j=0;j<i;j++)
{
  if(array[j]>array[j+1]){swap(&array[j],&array[j+1]);flag=true;}
}
if(!flag)break;
}
}
template<class ElemType>
void selectSort(ElemType array[],int n)
{
int minpos;
for(int i=0;i<n-1;i++)
{
minpos=i;
  for(int j=i;j<n;j++)
  {if(array[j]<array[minpos])minpos=j;}
swap(array[i],array[minpos]);
}
}
#define Left(i) (2*(i))
#define Right(i) (2*(i)+1)
template<class ElemType>
void heapAjust(ElemType array[],int m,int n)
{
ElemType tmp,max;bool flag;
if(Left(m)<n){
  if(array[Left(m)]>array[Right(m)]){max=array[Left(m)];flag=false;}
  else{max=array[Right(m)];flag=true;}
 
  if(array[m]>=max){return;}
  else{
      if(flag){swap(&array[Right(m)],&array[m]);heapAjust(array,Right(m),n);}
      else{swap(&array[Left(m)],&array[m]);heapAjust(array,Left(m),n);}
  }
}else if(Left(m)==n){
  if(array[Left(m)]>array[m])
  {
    swap(&array[Left(m)],&array[m]);
    heapAjust(array,Left(m),n);
  }
}else{
  return;
}

}
template<class ElemType>
void createHeap(ElemType array[],int m,int n)
{
if(Left(m)>n)return;
createHeap(array,Left(m),n);
createHeap(array,Right(m),n);
heapAjust(array,m,n);
}
template<class ElemType>
void heapSort(ElemType array[],int n)
{
createHeap(array,1,n);
for(int i=n;i>1;i--)
{
    swap(array[1],array[i]);
    heapAjust(array,1,i-1);
}
}
template<class ElemType>
void mergeSort(ElemType array[],int m,int n)
{
if(m==n){return;}
mergeSort(array,m,(n-m)/2+m);
mergeSort(array,m+(n-m)/2+1,n);
ElemType *base=new ElemType[n-m+1];

int i=m;
int j=m+(n-m)/2+1;
int k=0;
cout<<" i="<<i<<"j="<<j<<endl;
while(i<=m+(n-m)/2 && j<=n)
{
if(array[i]>array[j]){base[k]=array[j];j++;}
else{base[k]=array[i];i++;}
cout<<base[k]<<' '<<i<<' '<<j;
k++;
}
cout<<endl;
while(j<=n){base[k]=array[j];j++;k++;}
while(i<=m+(n-m)/2){base[k]=array[i];i++;k++;}
j=0;
for(i=m;i<=n;i++)
{
array[i]=base[j];
//cout<<base[j]<<' ';
j++;
}
cout<<endl;
delete base;
}

struct IntNode{int data;struct IntNode *next;};
void travserList(struct IntNode *head,struct IntNode *tail)
{
if(!tail)return;
while(head!=tail){cout<<head->data<<' ';head=head->next;}
cout<<head->data;
}
void baseSort(int array[],int n)
{
struct IntNode *head[10]={NULL};
struct IntNode *tail[10]={NULL};
struct IntNode *p=NULL,*q=NULL,*ListHead=NULL,*ListTail=NULL;
struct IntNode *node;
ListHead=ListTail=new struct IntNode;
ListTail->next=NULL;
int h=1,key,base,count;
for(int i=0;i<3;i++)
{
  for(int j=0;j<n;j++)
  {
      key=array[j]/h;
      base=key%10;
      node = new struct IntNode;
      node->data = array[j];node->next = NULL;
      if(!tail[base]){ListTail->next=node;ListTail=node;head[base]=node;tail[base]=node;}
      else{node->next=tail[base]->next;tail[base]->next=node;tail[base]=node;}
  }
  h*=10;
  count=0;
  for(int k=0;k<10;k++)
  {
     for(p=head[k];p!=tail[k]&&p;){array[count++]=p->data;p=p->next;}
     if(p)array[count++]=p->data;
  }
  for(p=ListHead->next;p;){q=p->next;delete p;p=q;}
  ListTail=ListHead;
  for(int t=0;t<10;t++){head[t]=tail[t]=NULL;}
}
}
int main(int argc,char *argv[])
{
int a[11]={8,7,8,2,9,1,4,5,2,10,6};
//heapSort(a,10);
//mergeSort(a,0,10);
baseSort(a,11);
for(int i=0;i<11;i++)
cout<<a[i]<<' ';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值