1 Asymptotics
(a) We have a function findMax that iterates through an unsorted int array once and returns the
maximum element found in that array. Give the tightest lower (Ω( · )) and upper bounds ( O ( · )) of
findMax in terms of N , the length of the array. Is it possible to define a Θ( · ) bound for findMax ?
(b) Give the worst case and best case runtime in terms of M and N . Assume ping is in Θ(1) and returns
an int .
1
for ( int i = N; i > 0; i--) {
2
for ( int j = 0; j <= M; j++) {
3
if (ping(i, j) > 64) break ;
4
}
5
}
(c) Below we have a function that returns true if every int has a duplicate in the array, and false if there
is any unique int in the array. Assume sort(array) is in Θ( N log N ) and returns array sorted.
1
public static boolean noUniques( int \[\] array) {
2
array = sort(array);
3
int N = array.length;
4
for ( int i = 0; i < N; i += 1) {
5
boolean hasDuplicate = false ;
6
for ( int j = 0; j < N; j += 1) {
7
if (i != j && arrayi == arrayj) {
8
hasDuplicate = true ;
9
}
10
}
11
if (!hasDuplicate) return false ;
12
}
13
return true ;
14
}
Give the worst case and best case runtime in Θ( · ) notation, where N = array.length .
Data Structures and Programming Methodology CS61
jia V iuww5202024-07-09 8:32
相关推荐
BK贝壳16 分钟前
初始化一亿个全 True 或全 False:bool-hybrid-array 的 TruesArray/FalsesArray 占位构造本机实测L-影37 分钟前
HashMap详解-上篇爱笑的源码基地42 分钟前
什么是智能预问诊系统?一套java版预问诊成品源码郝学胜-神的一滴1 小时前
Effective STL 条款 17:用 swap 技巧去掉多余容量她说..1 小时前
幂等问题分析玩转单片机与嵌入式1 小时前
深入浅出TinyML 36:如何验证一个TinyML系统真正具备工程应用价值?摇滚侠1 小时前
《On Java 中文版 基础卷》阅读笔记 集合 24小小测试开发1 小时前
rsync 源路径少一个斜杠,--delete 删的就不是你以为的那一层智购科技无人售货机工厂店2 小时前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH随流寻影2 小时前
利用 C++ 实现快速排序及三种优化