插入排序汇总

来源:互联网 发布:猫 知乎 编辑:程序博客网 时间:2024/06/06 08:34

 

#include<iostream>
using namespace std;

#define len 10
#define MAXINT 32767

void display(int *p){
    for(int i=0;i<len;i++)
        cout<<p[i]<<" ";
    cout<<endl;
    return ;
}

//直接插入排序
void insertSort(int *p){
     for(int i=1;i<len;i++){
         if(p[i]<p[i-1]){
             int temp = p[i],j;
             p[i] = p[i-1];
             for(j=i-2;temp<p[j] && j>=0;j--) //前面的数组是有序的,移动
                 p[j+1] = p[j];
             p[j+1] = temp;
         }
     }
     display(p);
     return ;
}

//折半插入排序
void binsertSort(int *p){
     for(int i=1;i<len;i++){
         int temp = p[i];
         int low = 0,high = i-1;
         while(low <= high){   //折半插入排序不用管相等情况,在于找最近的空档
             int m = (low + high)/2;
             if(temp < p[m]) high = m-1;
             else low = m+1;
         }
         for(int j=i-1;j>=high+1 && j>=0;j--) p[j+1] = p[j]; //移动
         p[high+1] = temp;
     }
    
     display(p);
     return ;
}

//2路折半插入排序
void brinsertSort(int *p){
     int assist[len]={0},first=0,final=len-1;
     assist[first] = p[0];assist[final] = p[len-1]; //初始化已放入两个元素
     for(int i=1;i<len/2;i++){ //前半段折半插入
         int temp = p[i];
         int low = 0,high = first;
         while(low <= high){
             int m = (low + high)/2;
             if(temp < assist[m]) high = m-1;
             else low = m+1;
         }
         for(int j=i-1;j>=high+1 && j>=0;j--) assist[j+1] = assist[j];
         assist[high+1] = temp;
         first ++;
     }
    
     for(int i=len/2;i<len-1;i++){ //后半段
         int temp = p[i];
         int low = final,high = len-1;
         while(low <= high){
             int m = (low + high)/2;
             if(temp < assist[m]) high = m-1;
             else low = m+1;
         }
         //下面这句千万要仔细
         for(int j=final-1;j<high && j<len-1;j++) assist[j] = assist[j+1];
         assist[high] = temp;
         final --;
     }
    
     int i,j;
     for( i=0,j=0;i<=first && final<len;){
         if(assist[i]<=assist[final]) p[j++] = assist[i++];
         else p[j++] = assist[final++];
     }
     for(;i<=first;) p[j++] = assist[i++];
     for(;final<len;) p[j++] = assist[final++];
    
     display(p);
     return ;
}

//表插入排序
void arrange(int *p){
     struct table{
            int data,next;
     }t[len+1];
     t[0].data = MAXINT;  //要比待排序序列的最大值大
     t[0].next = 1;
     for(int i=1;i<=len;i++)
         t[i].data = p[i-1];
     t[1].next = 0;   //初始化
     for(int i=2;i<=len;i++){
         int ti = t[0].next,titi=0; 
         //titi是ti的前驱 ,必须初始时即为前驱,否则。。。血的教训。。。
         while(t[ti].data <= t[i].data){   //修改指针指向
             titi = ti;
             ti = t[ti].next;
         }
         t[i].next = ti;
         t[titi].next = i;
     }
     for(int ti = t[0].next, i=0;ti!=0;ti = t[ti].next)//复制回去
     p[i++] = t[ti].data;
    
     display(p);
     return ;
}

//希尔排序
void ShellInsert(int *p,int d){
     for(int i=d+1;i<len;i++) 
         if(p[i]<p[i-d]){
             int j,temp = p[i];
             for(j=i-d;j>=0 && temp<p[j];j-=d)
                 p[j+d] = p[j];
             p[j+d] = temp;
         }
     return ;
}

void ShellSort(int *p){
    for(int i=5;i>=1;i-=2)
        ShellInsert(p,i);
    display(p);
    return ;
}

int main(void){
    int a[len]={3,6,2,1,8,4,5,9,0,7};
    //insertSort(a);
    //binsertSort(a);
    //brinsertSort(a);
    //arrange(a);
    ShellSort(a);
   
    system("pause");
}

原创粉丝点击