【排序】冒泡排序法and针对数组的普通查找法和二分查找法

下面是冒泡排序法的示例代码

冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。至此第一趟结束,将最大的数放到了最后。在第二趟:仍从第一对数开始比较(因为可能由于第2个数和第3个数的交换,使得第1个数不再小于第2个数),将小数放前,大数放后,一直比较到倒数第二个数(倒数第一的位置上已经是最大的),第二趟结束,在倒数第二的位置上得到一个新的最大数(其实在整个数列中是第二大的数)。如此下去,重复以上过程,直至最终完成排序。

[java]  view plain copy print ?
  1. package com.jadyer.sort;  
  2.   
  3. /** 
  4.  * 冒泡排序法 
  5.  * @see JDK本身也提供了数组排序API,不妨参考一下,即Arrays.sort(array) 
  6.  */  
  7. public class BubbleSort {  
  8.     public static void main(String[] args) {  
  9.         bubbleSort(new int[]{4,6,9,8,3,2});  
  10.     }  
  11.       
  12.     public static void bubbleSort(int[] array){  
  13.         for(int i=0; i<array.length-1; i++){  
  14.             for(int j=0; j<array.length-i-1; j++){  
  15.                 if(array[j] > array[j+1]){  
  16.                     int temp = array[j];  
  17.                     array[j] = array[j+1];  
  18.                     array[j+1] = temp;  
  19.                 }  
  20.             }  
  21.             System.out.print("这是第" + (i+1) + "次排序,排序结果为: ");  
  22.             for(int k=0; k<array.length; k++){  
  23.                 System.out.print(array[k] + " ");  
  24.             }  
  25.             System.out.println();  
  26.         }  
  27.     }  
  28. }  

下面是针对数组的普通查找法和二分查找法的示例代码

[java]  view plain copy print ?
  1. package com.jadyer.sort;  
  2.   
  3. /** 
  4.  * 数组查找方式 
  5.  * @detail 这里演示了普通查找法和二分查找法 
  6.  */  
  7. public class ArraySearch {  
  8.     public static void main(String[] args) {  
  9.         int commonResult = commonSearch(new int[]{1,5,6,7,4,3,9,11,13,14,16,19,21}, 9);  
  10.         int binaryResult = binarySearch(new int[]{1,3,4,6,7,8,9,12,15,17,18,20,22}, 8);  
  11.         System.out.println("二分查找法: " + binaryResult);  
  12.         System.out.println("普通查找法: " + commonResult);  
  13.     }  
  14.       
  15.     /** 
  16.      * 普通查找法 
  17.      * @detail 该方式最好理解,同时效率也最低 
  18.      */  
  19.     public static int commonSearch(int[] array, int value){  
  20.         for(int i=0; i<array.length; i++){  
  21.             if(value == array[i]){  
  22.                 return i; //返回该元素在数组中的下标  
  23.             }  
  24.         }  
  25.         return -1//不存在该元素则返回-1  
  26.     }  
  27.   
  28.     /** 
  29.      * 二分查找法 
  30.      * @detail 要求数组有序,升序或降序均可 
  31.      */  
  32.     public static int binarySearch(int[] array, int value){  
  33.         int low = 0//最小元素值的下标  
  34.         int high = array.length - 1//最大元素值的下标  
  35.         int middle; //中间元素的下标  
  36.         while(low <= high){  
  37.             middle = (low+high) / 2;  
  38.             for(int i=0; i<array.length; i++){  
  39.                 System.out.print(array[i]);  
  40.                 if(i == middle){  
  41.                     System.out.print("#"); //在元素后面用#号标识其为中间元素  
  42.                 }  
  43.                 System.out.print(" "); //各元素间用空格隔开  
  44.             }  
  45.             System.out.println();  
  46.             if(value == array[middle]){  
  47.                 return middle;  
  48.             }  
  49.             if(value < array[middle]){  
  50.                 high = middle - 1//右侧的不要了  
  51.             }  
  52.             if(value > array[middle]){  
  53.                 low = middle + 1//左侧的不要了  
  54.             }  
  55.         }  
  56.         return -1//不存在该元素则返回-1  
  57.     }  
  58. }  

你可能感兴趣的:(java,String,api)