首页 > 代码库 > 一种排序

一种排序

#include <iostream>
#include <stdio.h>

using namespace std;
int max(int a,int b){
    if(a>b) return a;
    return b;
}
int min(int a,int b){
    if(a>b) return b;
    return a;
}

void shu(int a[],int N)
{
     int a1,a2,a3;  int *bb = new int[N+2];
    //转入第一个元素
    for(int j=4;j<N;j=j+3){
        int i =j;
        while(a[i]<a[i-3]&&i>=4){
            a1=a[i-3];  a2=a[i-2]; a3=a[i-1];
            a[i-3]=a[i]; a[i-2]=a[i+1]; a[i-1]=a[i+2];
            a[i]=a1; a[i+1]=a2;a[i+2]=a3;
            i=i-3;
        }
    }

     for(int jJ=4;jJ<N;jJ=jJ+3){
            int i = jJ;  //费了很多时间,你是想用上面的那个插入方法,while里面会用很多的判断
            if(a[i]>a[i-3])  continue;//不用交换了
            if((a[i]==a[i-3]) && max(a[i+1],a[i+2])>max(a[i-2],a[i-1]) ) continue;
            if((a[i]==a[i-3]) && max(a[i+1],a[i+2])==max(a[i-2],a[i-1]) &&  min(a[i+1],a[i+2])>=min(a[i-2],a[i-1]) ) continue;

            while(i>=4){
            if(a[i]>a[i-3]) {i=i-3; continue;}//不用交换了
            if((a[i]==a[i-3]) && max(a[i+1],a[i+2])>max(a[i-2],a[i-1]) ) {i=i-3;continue;}
            if((a[i]==a[i-3]) && max(a[i+1],a[i+2])==max(a[i-2],a[i-1]) &&  min(a[i+1],a[i+2])>=min(a[i-2],a[i-1]) ) {i=i-3;continue;}
            int a1,a2,a3;
            a1=a[i-3];  a2=a[i-2]; a3=a[i-1];
            a[i-3]=a[i]; a[i-2]=a[i+1]; a[i-1]=a[i+2];
            a[i]=a1; a[i+1]=a2;a[i+2]=a3;
            i=i-3;
            }
      }

}



int main()
{   int N; cin>>N;
    for(int kkk=1;kkk<=N;kkk++){
        int num; cin>>num;
        int *a=new int[3*num+1];
        for(int u=1;u<3*num+1;u++){
            cin >> a[u];
        }
        int *b = new int[3*num+2];

        shu(a,3*num+1);
        b[1]=a[1];b[2]=a[2];b[3]=a[3]; int k=4;




    for(int i=4;i<3*num+1;i=i+3){
        if(  a[i]==b[k-3]  &&  max(a[i+1],a[i+2])==max(b[k-2],b[k-1]) && min(a[i+1],a[i+2])==min(b[k-2],b[k-1]) ){continue;}
        b[k]=a[i];b[k+1]=a[i+1];b[k+2]=a[i+2]; k=k+3;
    } b[k]=-100;

    int hh=1;
    while(b[hh]!=-100){
        cout <<b[hh]<<" ";
        if(b[hh+1]<b[hh+2])
        {
            cout<<b[hh+2]<<" ";
            cout<<b[hh+1]<<" ";
        }
        else {cout<<b[hh+1]<<" ";cout<<b[hh+2]<<" "; }
         printf("\n"); hh=hh+3;
    }
    }
    return 0;

}

  

 //按照第2,3序列进行排序
     //       if(   a[j]>a[j-3]  ||  max(a[j+1],a[j+2])>max(a[j-2],a[j-1])      jixu                  )
 //       if(max(a[j+1],a[j+2])=max(a[j-2],a[j-1]) && dayudeng)
      for(int jJ=4;jJ<N;jJ=jJ+3){

        int i =jJ;
//int flag=  max(a[i],a[i+2])<max(a[i-2],a[i-1]);
int flag1= max(a[i],a[i+2])==max(a[i-2],a[i-1])&& max(a[i+1],a[i+2])==max(a[i-2],a[i-1]) && min(a[i+1],a[i+2])<min(a[i-2],a[i-1]);
int flag2 = max(a[i],a[i+2])==max(a[i-2],a[i-1])&&max(a[i+1],a[i+2])<max(a[i-2],a[i-1]) ;
        while(  (a[i]<a[i-3]||flag1==1||flag2==1)&&i>=4){
            //移动,然后进行
            int a1,a2,a3;
            a1=a[i-3];  a2=a[i-2]; a3=a[i-1];
            a[i-3]=a[i]; a[i-2]=a[i+1]; a[i-1]=a[i+2];
            a[i]=a1; a[i+1]=a2;a[i+2]=a3;
            i=i-3;

//flag=  max(a[i+1],a[i+2])<max(a[i-2],a[i-1]);
int flag1= max(a[i],a[i+2])==max(a[i-2],a[i-1])&& max(a[i+1],a[i+2])==max(a[i-2],a[i-1]) && min(a[i+1],a[i+2])<min(a[i-2],a[i-1]);
int flag2 = max(a[i],a[i+2])==max(a[i-2],a[i-1])&&max(a[i+1],a[i+2])<max(a[i-2],a[i-1]) ;

        }
    }

 

一种排序