冒泡排序算法的一般性策略:搜索整個值列,比較相鄰元素,如果兩者的相對次序不對,則交換它們,其結果是最大值“想水泡一樣”移動到值列的最后一個位置上,這也是它在最終完成排序的值列中合適的位置。然后再次搜索值列,將第二大的值移動至倒數第二個位置上,重復該過程,直至將所有元素移動到正確的位置上。
下面是兩個Java冒泡算法程序
2、冒泡代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package org.cyxl.algorithm.search; /** * 二分查找 * @author cyxl * */ public class BinarySearch { private int rCount= 0 ; private int lCount= 0 ; /** * 獲取遞歸的次數 * @return */ public int getrCount() { return rCount; } /** * 獲取循環的次數 * @return */ public int getlCount() { return lCount; } /** * 執行遞歸二分查找,返回第一次出現該值的位置 * @param sortedData 已排序的數組 * @param start 開始位置 * @param end 結束位置 * @param findValue 需要找的值 * @return 值在數組中的位置,從0開始。找不到返回-1 */ public int searchRecursive( int [] sortedData, int start, int end, int findValue) { rCount++; if (start<=end) { //中間位置 int middle=(start+end)>> 1 ; //相當于(start+end)/2 //中值 int middleValue=sortedData[middle]; if (findValue==middleValue) { //等于中值直接返回 return middle; } else if (findValue<middleValue) { //小于中值時在中值前面找 return searchRecursive(sortedData,start,middle- 1 ,findValue); } else { //大于中值在中值后面找 return searchRecursive(sortedData,middle+ 1 ,end,findValue); } } else { //找不到 return - 1 ; } } /** * 循環二分查找,返回第一次出現該值的位置 * @param sortedData 已排序的數組 * @param findValue 需要找的值 * @return 值在數組中的位置,從0開始。找不到返回-1 */ public int searchLoop( int [] sortedData, int findValue) { int start= 0 ; int end=sortedData.length- 1 ; while (start<=end) { lCount++; //中間位置 int middle=(start+end)>> 1 ; //相當于(start+end)/2 //中值 int middleValue=sortedData[middle]; if (findValue==middleValue) { //等于中值直接返回 return middle; } else if (findValue<middleValue) { //小于中值時在中值前面找 end=middle- 1 ; } else { //大于中值在中值后面找 start=middle+ 1 ; } } //找不到 return - 1 ; } } |
4、測試代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package org.cyxl.algorithm.search.test; import org.cyxl.algorithm.search.BinarySearch; import org.junit.Test; public class BinarySearchTest { @Test public void testSearch() { BinarySearch bs= new BinarySearch(); int [] sortedData={ 1 , 2 , 3 , 4 , 5 , 6 , 6 , 7 , 8 , 8 , 9 , 10 }; int findValue= 9 ; int length=sortedData.length; int pos=bs.searchRecursive(sortedData, 0 , length- 1 , findValue); System.out.println( "Recursice:" +findValue+ " found in pos " +pos+ ";count:" +bs.getrCount()); int pos2=bs.searchLoop(sortedData, findValue); System.out.println( "Loop:" +findValue+ " found in pos " +pos+ ";count:" +bs.getlCount()); } } |
5、總結:這種查找方式的使用場合為已排序的數組。可以發現遞歸和循環的次數是一樣的
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/liu_jing_hui/article/details/52944328