C二进制搜索数组问题 [英] C Binary search array question

查看:90
本文介绍了C二进制搜索数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个例如4,3,17,5,1,2

的数组,输出应该是:17-1 = 16,因为它是数字之间的最大差异。

i想出了怎么做,但我不明白的是,为什么我的老师告诉我对数组进行排序并应用二进制搜索,它应该是O(nlogn)。我不明白有什么不同,如果我做线性,只是比较最大差异与其他所有,那就是全部,为什么我需要对它进行排序???

解决方案

排序algorhitm有不同的复杂性。在维基百科中,您可以找到一些解释。



如果你已排序的数据,您可以采取第一个和最后一个元素。你已经完成了。



你的老师希望你了解它。 :-O

So i have an array for example 4,3,17,5,1,2
and the output should be: 17-1=16 since it is the max difference between the numbers.
i figured it out how to do it, but what I don't understand is, why my teacher is telling me to sort the array and to apply binary search, it should be O(nlogn). I don't understand what is the difference if i do it linear, just comparing the max diff with every other ones and thats all, why i need to sort it???

解决方案

Sorting algorhitm have different complexitiy. In the wikipedia you find some explanations.

If you have sorted data you can take the first and the last element. And you are done.

Your teachers wants you to learn about it. :-O


这篇关于C二进制搜索数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆