java 查找算法_常见查找算法(Java实现)

查找的性能分析:

对于查找算法而言,常用“其关键字和给定值进行过比较的记录个数的平均值”作为衡量查找算法的依据。

定义:为了确定记录在查找表中的位置,需要和给定的值进行比较的关键字个数的期望值称为查找算法在查找成功时的平均查找长度。

对于含有n个记录的表。查找成功时的平均查找长度为

ASL=∑i=1nPiCiASL=∑i=1nPiCi其中PiPi为查找表中第ii个记录的概率,且

∑i=1nPi=1∑i=1nPi=1CiCi为找到表中关键字与给定值相等的第i个记录时,和给定值已经进行过比较的关键字个数。

静态表查找

顺序查找:

算法描述:从表中的第一个或者是最后一个记录开始,逐个的将表中记录的关键字和给定值进行比较。若某个记录的关键字和给定值相等,则查找成功;若表中所有记录的关键字和给定值都不相等,则查找失败。

算法实现:

public class OrderSearch {

public static void main(String[] args) {

int[] table = {1,23,4,5,6};

System.out.println(OrderSearch.orderSearch(table, 5));

}

/**

* 顺序查找

* @param table

* @param keyWord

* @return

*/

public static boolean orderSearch(int[] table,int keyWord){

for(int i = 0;i

if(table[i] == keyWord){

return true;

}

}

return false;

}

}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

折半查找(针对有序序列):

算法描述:折半查找的前提条件是在一个有序的序列中。首先确定待查记录所在的区间,然后逐步的缩小范围区间直到找到或者找不到该记录为止。与数学中的二分法一样。

算法实现:

public class BinarySearch {

public static void main(String[] args) {

int[] table = {2,4,5,6,7,8,9,10};

System.out.println(BinarySearch.binarySearch(table, 20));

}

public static boolean binarySearch(int[] table ,int keyWord){

int low = 0;

int height = table.length-1;

int mid;

while(low<=height){

mid = (low+height)/2;

if(table[mid]> keyWord){

height = mid-1;

}else if(table[mid]

low = mid+1;

}else if(table[mid] == keyWord){

return true;

}

}

return false;

}

}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

斐波那契查找(针对有序序列):

算法描述:斐波那契查找是根据斐波那契序列的特点对表进行分割。假设表中记录的个数比某个斐波那契数小1,即n=Fn−1n=Fn−1,然后将给定值和表中第Fn−1Fn−1个记录的关键字进行比较。若相等,则查找成功;若给定关键字<>表中第Fn−1Fn−1个记录的关键字,则继续在自Fn−1+1Fn−1+1至Fn−1Fn−1的子表中查找。

算法实现:

public class FibonacciSearch {

public static void main(String[] args) {

int[] table = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};

for(int i = 0;i

System.out.println("表中记录为"+table[i]+"\t,查询结果为"+FibonacciSearch.fibonacciSearch(table, table[i])+"\n");

}

for(int i = 0;i<5;i++){

int ran = new Random().nextInt(100)+14;

System.out.println("关键字为:"+ran+"查询结果为"+FibonacciSearch.fibonacciSearch(table, ran)+"\n");

}

}

public static boolean fibonacciSearch(int[] table,int keyWord){

//确定需要的斐波那契数

int i = 0;

while(getFibonacci(i)-1 == table.length){

i++;

}

//开始查找

int low = 0;

int height = table.length-1;

while(low<=height){

int mid = low + getFibonacci(i-1);

if(table[mid] == keyWord){

return true;

}else if(table[mid]>keyWord){

height = mid-1;

i--;

}else if(table[mid]

low = mid+1;

i-=2;

}

}

return false;

}

/**

* 得到第n个斐波那契数

* @return

*/

public static int getFibonacci(int n){

int res = 0;

if(n == 0){

res = 0;

}else if(n == 1){

res = 1;

}else{

int first = 0;

int second = 1;

for(int i = 2;i<=n;i++){

res = first+second;

first = second;

second = res;

}

}

return res;

}

}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

动态表查找

二叉排序树(二叉查找树):

定义:二叉排序树或者是空树,或者是具有下列性质的一颗树:

若他的左子树不为空,则左子树上所有结点的值均小于其根节点的值

若他的右子树不为空,则右子树上所有结点的值均大于其根节点的值

他的左右子树也均是二叉排序树

由于二叉排序树的特性,使得我们对其进行中序遍历时就很容易得到一个非递减的有序序列,因此二叉排序树也经常用于排序操作。

参考:《数据结构》(C语言版)严蔚敏 吴伟明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值