二进制搜索算法的平均性能?

编程入门 行业动态 更新时间:2024-10-07 16:17:29
本文介绍了二进制搜索算法的平均性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

en.wikipedia/wiki/Binary_search_algorithm#Average_performance

BinarySearch(int A[], int value, int low, int high) { int mid; if (high < low) return -1; mid = (low + high) / 2; if (A[mid] > value) return BinarySearch(A, value, low, mid-1); else if (A[mid] < value) return BinarySearch(A, value, mid+1, high); else return mid; }

如果整我想找到的是始终在数组中,任何人都可以帮我写一个程序,可以计算二进制搜索算法的平均性能?

If the integer I'm trying to find is always in the array, can anyone help me write a program that can calculate the average performance of binary search algorithm?

编辑:我知道我可以通过实际运行程序和计算呼叫的次数做到这一点,但我想要做的,是做不调用函数

edit: I know I can do this by actually running the program and counting the number of calls, but what I'm trying to do here is to do it without calling the function.

EDIT2:KennyTM:这是一个时间复杂度,我试图计算呼叫的平均数量。例如,呼叫的平均数量来查找A中的整数[2],这将是1.67(5/3)

edit2: KennyTM: That is a time complexity, I'm trying to calculate the average number of calls. For example, the average number of calls to find a integer in A[2], it would be 1.67 (5/3)

推荐答案

您并不需要一个节目。你可以仅仅指望调用二分查找方法的数目。

You don't need a "program". You can just count the number of calls to the BinarySearch method.

您可以做到这一点很容易地通过传递另一个参数(通过指针),或使用全局变量。在这种情况下 - 这是一个玩具 - 所以我可能会去快速和肮脏与全球

You can do that easily by passing another parameter (by pointer) or using a global variable. In this case - it's a toy - so I'd probably go quick and dirty with the global.

更多推荐

二进制搜索算法的平均性能?

本文发布于:2023-11-29 02:23:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   平均   性能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!