冒泡、二分、快速

来源:互联网 发布:淘宝返利机器人多少钱 编辑:程序博客网 时间:2024/05/17 04:42

1、冒泡算法的原理:

冒泡排序算法的一般性策略:搜索整个值列,比较相邻元素,如果两者的相对次序不对,则交换它们,其结果是最大值“想水泡一样”移动到值列的最后一个位置上,这也是它在最终完成排序的值列中合适的位置。然后再次搜索值列,将第二大的值移动至倒数第二个位置上,重复该过程,直至将所有元素移动到正确的位置上。
下面是两个Java冒泡算法程序
2、冒泡代码如下:

public class BubbleSort {    public static void bubbleSort(int[] a) {        int temp;        for (int i = 0; i < a.length - 1; ++i) {            for (int j = a.length - 1; j > i; --j) {                if (a[j] < a[j - 1]) {                    temp = a[j];                    a[j] = a[j - 1];                    a[j - 1] = temp;                }            }        }    }    public static void main(String[] args) {        int a[] = { 49,38,65,97,76,13,27,49};        bubbleSort(a);        System.out.println(Arrays.toString(a));    }} 

2、二分算法
(1)前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序
(2)原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位置的那个值)前,中值,中值后;将要查找的值和数组的中值进行比较,若小于中值则在中值前面找,若大于中值则在中值后面找,等于中值时直接返回。然后依次是一个递归过程,将前半部分或者后半部分继续分解为三部分。可能描述得不是很清楚,若是不理解可以去网上找。从描述上就可以看出这个算法适合用递归来实现,可以用递归的都可以用循环来实现。所以我们的实现分为递归和循环两种,可以根据代码来理解算法

(3)实现:代码如下

  1. package org.cyxl.algorithm.search;    2.     3. /**   4.  * 二分查找   5.  * @author cyxl   6.  *   7.  */    8. public class BinarySearch {    9.     private int rCount=0;    10.     private int lCount=0;    11.         12.     /**   13.      * 获取递归的次数   14.      * @return   15.      */    16.     public int getrCount() {    17.         return rCount;    18.     }    19.     20.     /**   21.      * 获取循环的次数   22.      * @return   23.      */    24.     public int getlCount() {    25.         return lCount;    26.     }    27.  /**   28.      * 执行递归二分查找,返回第一次出现该值的位置   29.      * @param sortedData    已排序的数组   30.      * @param start         开始位置   31.      * @param end           结束位置   32.      * @param findValue     需要找的值   33.      * @return              值在数组中的位置,从0开始。找不到返回-1   34.      */    35.     public int searchRecursive(int[] sortedData,int start,int end,int findValue)    36.     {    37.         rCount++;    38.         if(start<=end)    39.         {    40.             //中间位置    41.             int middle=(start+end)>>1;    //相当于(start+end)/2    42.             //中值    43.             int middleValue=sortedData[middle];    44.                 45.             if(findValue==middleValue)    46.             {    47.                 //等于中值直接返回    48.          return middle;    49.             }    50.             else if(findValue<middleValue)    51.             {    52.                 //小于中值时在中值前面找    53.                 return searchRecursive(sortedData,start,middle-1,findValue);    54.             }    55.             else    56.             {    57.                 //大于中值在中值后面找    58.                 return searchRecursive(sortedData,middle+1,end,findValue);    59.             }    60.         }    61.         else    62.         {    63.             //找不到    64.             return -1;    65.         }    66.     }    67.   /**   68.      * 循环二分查找,返回第一次出现该值的位置   69.      * @param sortedData    已排序的数组   70.      * @param findValue     需要找的值   71.      * @return              值在数组中的位置,从0开始。找不到返回-1   72.      */    73.     public int searchLoop(int[] sortedData,int findValue)    74.     {    75.         int start=0;    76.         int end=sortedData.length-1;    77.             78.         while(start<=end)    79.         {    80.             lCount++;    81.             //中间位置    82.             int middle=(start+end)>>1;    //相当于(start+end)/2    83.             //中值    84.             int middleValue=sortedData[middle];    85.                 86.             if(findValue==middleValue)    87.             {    88.                 //等于中值直接返回    89.                 return middle;    90.             }    91.    else if(findValue<middleValue)    92.             {    93.                 //小于中值时在中值前面找    94.                 end=middle-1;    95.             }    96.             else    97.             {    98.                 //大于中值在中值后面找    99.                 start=middle+1;    100.             }    101.         }    102.         //找不到    103.         return -1;    104.     }    105. }  

4、测试代码

  1. package org.cyxl.algorithm.search.test;    2.     3. import org.cyxl.algorithm.search.BinarySearch;    4. import org.junit.Test;    5.     6.     7. public class BinarySearchTest {    8.     @Test    9.     public void testSearch()    10.     {    11.         BinarySearch bs=new BinarySearch();    12.             13.         int[] sortedData={1,2,3,4,5,6,6,7,8,8,9,10};    14.         int findValue=9;    15.         int length=sortedData.length;    16.             17.         int pos=bs.searchRecursive(sortedData, 0, length-1, findValue);    18.         System.out.println("Recursice:"+findValue+" found in pos "+pos+";count:"+bs.getrCount());    19.         int pos2=bs.searchLoop(sortedData, findValue);    20.             21.         System.out.println("Loop:"+findValue+" found in pos "+pos+";count:"+bs.getlCount());    22.     }    23. }  

5、总结:这种查找方式的使用场合为已排序的数组。可以发现递归和循环的次数是一样的

0 0
原创粉丝点击