常用排序、查找算法java实现

1、冒泡排序

public static voidbubbleSort(int[] a) {
       boolean change = true;
       for(int i=a.length-1; i >= 0 && change; i--) {
           change = false;
           for(int j=0; j < i; j++) {
              if(a[j] > a[j+1]) {
                  Sort.swap(a,j,j+1);
                  change = true;
              }
           }
       }
       print(a);
    }


      

2、选择排序

       publicstatic void select(int a[]) {

       for(int i=0; i < a.length-1; i++) {

           int min = i;

           for(int j = i+1; j < a.length; j++) {

              if(a[j] < a[min]) {

                  min = j;

              }

           }

           if(i != min) {

              swap(a, i, min);

           }

       }

       print(a);

    }

   

      3、插入排序

    public static void insert(int a[]) {

       int i,j,k = 0;

       for(i=0; i < a.length; i++){

           k = a[i];

           for(j = i-1; j>=0 && k<a[j]; j--){

              a[j+1] = a[j];

           }

           a[j+1] = k;

       }

       print(a);

    }

 

4、折半插入排序

public static void binsertSort(inta[]) {

       for(int i=1; i<a.length; i++) {

           int tempValue = a[i];

           int low = 0;

           int high = i - 1;

           while(low <= high) {

              int mid = (low+high)/2;

              if(tempValue < a[mid])

                  high = mid - 1;

              else

                  low = mid + 1;

           }

           for(int j=i-1;j>=high+1;j--) {

              a[j+1] = a[j];

           }

           a[high+1]=tempValue;

       }

    }

 

5、快速排序

public static int adjust(inta[],int low,int high) {

       int pivotkey = a[low];

       while(low < high) {

           while(low < high && a[high] > pivotkey) {

              high--;

           }

           a[low] = a[high];

           while(low < high && a[low] < pivotkey) {

              low++;

           }

           a[high] = a[low];

       }

       a[low] = pivotkey;

       return low;

    }

   

    public static void quickSort(int a[],int low, int high) {

       if(low < high) {

           int povitkeyPosition = adjust(a, low, high);

           quickSort(a, low, povitkeyPosition-1);

           quickSort(a, povitkeyPosition+1, high);

       }

    }

 

6、二分查找

public static StringBsearch(int a[],int target) {

       int low = 0;

       int high = a.length-1;

       while(low <= high) {

           int mid = (low + high)/2;

           int midValue = a[mid];

           if(midValue < target)

              low = mid + 1;

           else if(midValue > target)

              high = mid - 1;

           else {

              String str = "exists:";

              for(int i=0; i<a.length; i++) {

                  if(a[i] == a[mid])

                     str += " "+i;

              }

              return str;

           }

       }

       return "not exists";

    }

   

    public static String Bsearch2(int a[],int target,int low,inthigh) {

       int mid;

       if(low > high) return "not exists";

       mid = (low + high)/2;

       if(a[mid] > target) {

           return(Bsearch2(a, target, low, mid-1));

       }

       if(a[mid] < target) {

           return(Bsearch2(a, target, mid+1, high));

       }

       else {

           String str = "exists:";

           for(int i=0; i<a.length; i++) {

              if(a[i] == a[mid])

                  str += " "+i;

           }

           return str;

       }

    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值