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
相关推荐
顧棟20 分钟前
组件生命周期设计:从硬编码初始化到状态机Pniubi39 分钟前
01背包类问题-力扣416.分割等和子集特立独行的猫A8 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解一起_java8 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现Flynn Liang10 小时前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS吃我一窝jio11 小时前
Python与Go线程模型深度对比这个昵称不容易11 小时前
SpringBoot 购物推荐网站的设计与实现:120 个 Java 文件、147 个接口、13 张表,附真实运行截图文丑颜不良啊11 小时前
LeetCode3041~3060题目解析eggwyw11 小时前
C++ union 的一种妙用法罗西的思考12 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示