java 二分法算法的实例

2025-05-29 0 76

java 二分法算法的实例

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

106

107
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、总结:这种查找方式的使用场合为已排序的数组。可以发现递归和循环的次数是一样的

以上就是java 二分法的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://wely.iteye.com/blog/2329409

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 二分法算法的实例 https://www.kuaiidc.com/115037.html

相关文章

发表评论
暂无评论