常用排序算法的java实现

java语言,eclipse开发环境

 

//快速排序 

public static void sort(int[] a,int left,int right){
      int i=left;
      int j=right;
      int temp;
      if(left<right){
                 temp=a[left];
                 while(i!=j){
                           while(j>i&&a[j]>=temp){
                                 j--;
                            }
                           if(i<j){
                                 a[i]=a[j];
                                  i++;
                            }
                             while(i<j&&a[i]<=temp){
                                    i++;
                            }
                           if(i<j){
                                  a[j]=a[i];
                                   j--;
                            }
               }      
      }
      else{
              return;
        }
       a[i]=temp;  
       sort(a,left,i-1);
       sort(a,i+1,right);  
 }

//冒泡排序 

public static void sort(int[] a){
  
    boolean flag=true;  
    for(int i=0;i<a.length-1;i++){   
            for(int j=1;j<a.length-i;j++){
    
                    if(a[j-1]>a[j]){
                            flag=false;
                            int temp=a[j-1];
                            a[j-1]=a[j];
                            a[j]=temp;
                    }
              }
   
              if(flag==true){
                     // System.out.println("It's odered");
                      break;
               }
    
     }
 }

 

//插入排序

public static void sort(int[] a){
  int temp, j;
  for(int i=1;i<a.length;i++){
             temp=a[i];
              j=i-1;
             while(j>=0&&temp<a[j]){
              //while(temp<a[j]){ //error
                          a[j+1]=a[j];
                           j--;
               }
              a[j+1]=temp;
   }
 }

//二分插入排序

public static void sort(int[] a){
  int low,mid,high,temp;
  for(int i=1;i<a.length;i++){
           low=0;
           high=i-1;
          //mid=(low+high)/2;
           temp=a[i];
            while(low<=high){
                  mid=(low+high)/2;
                  if(temp>a[mid]){
                             low=mid+1;
                   }
                  else{
                           high=mid-1;
                   }
               }
             for(int j=i-1;j>=high+1;j--){
                    a[j+1]=a[j];
              }
             a[high+1]=temp;
    }
 }


 //测试程序
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  int[] a={8,9,5,7,2,4,1,6,3,0};
  //int[] a={1,2,3,4,5,6,7,8,9};
  System.out.println("before sort:");
  for(int i=0;i<a.length;i++){
          System.out.print(a[i]+" ");
  }
  System.out.println();
  
  sort(a);
  //sort(a,0,a.length-1);//quick_sort


  System.out.println("after sort:");
  for(int i=0;i<a.length;i++){
           System.out.print(a[i]+" ");
  }
  System.out.println();
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值