• Java实现冒泡排序、折半查找


    1、冒泡排序

    public class BubbleSort{  
          public static void main(String[] args){  
              int score[] = {67, 69, 75, 87, 89, 90, 99, 100};  
              for (int i = 0; i < score.length -1; i++){    //最多做n-1趟排序  
                  for(int j = 0 ;j < score.length - i - 1; j++){    //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)  
                      if(score[j] < score[j + 1]){    //把小的值交换到后面  
                          int temp = score[j];  
                          score[j] = score[j + 1];  
                          score[j + 1] = temp;  
                      }  
                  }              
                  System.out.print("第" + (i + 1) + "次排序结果:");  
                  for(int a = 0; a < score.length; a++){  
                      System.out.print(score[a] + "	");  
                  }  
                  System.out.println("");  
              }  
                  System.out.print("最终排序结果:");  
                  for(int a = 0; a < score.length; a++){  
                      System.out.print(score[a] + "	");  
             }  
          }  
      } 

    2、折半查找

    public class BinarySearch { 
        /** 
        * 二分查找算法 
        * 
        * @param srcArray 有序数组 
        * @param key 查找元素 
        * @return key的数组下标,没找到返回-1 
        */  
        public static void main(String[] args) { 
            int srcArray[] = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101};   
            System.out.println(binSearch(srcArray, 0, srcArray.length - 1, 81));  
        } 
         
        // 二分查找递归实现   
        public static int binSearch(int srcArray[], int start, int end, int key) {   
            int mid = (end - start) / 2 + start;   
            if (srcArray[mid] == key) {   
                return mid;   
            }   
            if (start >= end) {   
                return -1;   
            } else if (key > srcArray[mid]) {   
                return binSearch(srcArray, mid + 1, end, key);   
            } else if (key < srcArray[mid]) {   
                return binSearch(srcArray, start, mid - 1, key);   
            }   
            return -1;   
        } 
         
        // 二分查找普通循环实现   
        public static int binSearch(int srcArray[], int key) {   
            int mid = srcArray.length / 2;   
            if (key == srcArray[mid]) {   
                return mid;   
            }   
      
            int start = 0;   
            int end = srcArray.length - 1;   
            while (start <= end) {   
                mid = (end - start) / 2 + start;   
                if (key < srcArray[mid]) {   
                   end = mid - 1;   
                } else if (key > srcArray[mid]) {   
                    start = mid + 1;   
                } else {   
                    return mid;   
                }   
            }   
            return -1;   
        } 
    } 

    计算中间位置使用:

    int mid = (end - start) / 2 + start; 

    防止加法运算时可能导致的整数越界

  • 相关阅读:
    线上崩溃... MediaPlayerNative: error (1, -2147483648)
    Android Notification cancel和cancelAll方法无效问题
    Android ButterKnife 找不到符号
    Android日历组件
    Android dimen适配
    Android View的invalidate()原理
    Android Camera坐标系三维图及正向绕行方向
    Android 自定义多选的 Spinner
    Android中PorterDuff.Mode【转】
    java面向对象的注意点2
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/5728780.html
Copyright © 2020-2023  润新知