排序算法

//快速排序算法,以一个数为key,把比它大的放右边,比它小的放左边。这样分为两部分,再对两部分分别进行相同的操作,最终得到所要的结果。以下是我的java实现,并带有一个数组的测试。

public class QuickArrag {
     int count=0;
     public QuickArrag(){}
     public int quickSortBack(int[] m,int location,int to){
         int key=m[location];
         int j=to;
         for(;j>location;j--){
         if(m[j]<key){
            m[location]=m[j];
            m[j]=key;
            break;
            }
        }return j;
     }
     public int quickSortForward(int[] m,int location,int from){
         int key=m[location];
         int i=from;
         for(;i<location;i++){
             if(m[i]>key){
                m[location]=m[i];
                m[i]=key;
                break;
               }
            }
         return i;
     }
     public int quickSortOne(int[] m,int from,int to){
         int temp1=0;
         int temp2=1;
         int location=0;
         while(temp1!=temp2){
         location=quickSortBack(m,location,to);
         temp1=location;
         location=quickSortForward(m,location,from);
         temp2=location;
         }
         return temp2;
     }
     public void quickSort(int[] m,int from,int to){
         int location=from;
         location=quickSortOne(m,location,to);
         if(location>from){
         quickSort(m,from,location-1);
         }
         if(location<to){
         quickSort(m,location+1,to);
         } 
     }
    public static void main(String[] arg){
        int m[]={49,38,65,97,76,13,27,7};
        QuickArrag quickArray=new QuickArrag();
        quickArray.quickSort(m, 0, 7);
        for(int i=0;i<8;i++){
            System.out.println(m[i]+" ");
        }
    }
}

//冒泡排序复杂度n*n,利用count统计移动次数 12
/**
public class choiceArrage {
     
    public int choiceSort(int[] m,int length){
        int count=0;
      for(int i=0;i<length;i++)
         for(int j=i+1;j<length;j++)
             if(m[i]>m[j]){
                int temp=m[i];
                    m[i]=m[j];
                    m[j]=temp;
                    count++;
             }
      return count;
  }
  public static void main(String[] arg){
      int m[]={49,38,65,97,76,13,27};
      int count=new choiceArrage().choiceSort(m,7);
      for(int i=0;i<7;i++){
          System.out.println(m[i]+" ");
      } System.out.println(count+" ");
  }
}
*/
/**
//选择排序,利用count统计移动次数 7
public class choiceArrage {
    public int choiceSort(int[] m,int length){
        int count=0;
        int temp;
        int min=0;
        int index=0;
        for(int i=0;i<length;i++)
         {
            min=m[i];
            index=i;
            for(int j=i;j<length;j++)
             {
               
               if(min>m[j]){
                   min=m[j];
                   index=j;
                   }
               }
            temp=min;
            m[index]=m[i];
            m[i]=temp;     
            count++;//统计交换次数
       }
        return count;
}
     public static void main(String[] arg){
              int m[]={49,38,65,97,76,13,27};
              int count=new choiceArrage().choiceSort(m,7);
              for(int i=0;i<7;i++){
                  System.out.println(m[i]+" ");
              }System.out.println(count+" ");
         }
}
 
*/
//插入排序 12次
/**
public class choiceArrage {
    public int choiceSort(int[] m,int length){
        int count=0;
        int temp=m[0];
        for(int i=1;i<length;i++)
            for(int j=i;j>0;j--)
            if(m[j]<m[j-1]){
                temp=m[j];
                m[j]=m[j-1];
                m[j-1]=temp;
                count++;
            }
        return count;
}
     public static void main(String[] arg){
              int m[]={49,38,65,97,76,13,27};
              int count=new choiceArrage().choiceSort(m,7);
              for(int i=0;i<7;i++){
                  System.out.println(m[i]+" ");
              }System.out.println(count+" ");
         }
}
*/
//shell排序,6次
/**
public class choiceArrage {
    public int choiceSortOne(int[] m,int length,int interval){
        int count=0;
        int temp=m[0];
        for(int i=0;i<interval;i++)
        {
            for(int j=i+interval;j<length;j+=interval){
                for(int k=j;k>interval-1;k-=interval)
                    if(m[k]<m[k-interval]){
                    temp=m[k];
                    m[k]=m[k-interval];
                    m[k-interval]=temp;     
                    count++;
                }
            }
        }
        return count;
      }
     
    public int choiceSort(int[] m,int length){
        int count=0;
        int count1=0;
        for(int interval=length/2;interval>0;interval=interval/2){
            count1=choiceSortOne(m,length,interval);
            count+=count1;
        }
      return count;
  }
  public static void main(String[] arg){
      
      int m[]={49,38,65,97,76,13,27};
      int count=new choiceArrage().choiceSort(m,7);
      for(int i=0;i<7;i++){
          System.out.println(m[i]+" ");
      } System.out.println(count+" ");
  }
}
*/
//合并排序
/**
先对各个部分进行排序,然后一第一个有序列基准
,用第二个有序列来和他比较,结果放入第二个数组中暂时存起来,最后再复制过来
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值