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
相关推荐
大黄说说12 小时前
PHP 异步怎么实现?Swoole、Workerman 使用场景对比十二同学啊12 小时前
Python 工程化详解:项目结构、依赖管理、代码质量、测试与部署YIN_尹12 小时前
【git原理与使用】git基本操作及底层原理(一)乐橙开放平台12 小时前
跨地域监控难管理?通过 GB28181 把各地海康大华乐橙摄像头注册到云端平台广东融科数据服务有限公司12 小时前
网络监控经常卡顿、掉线、画面模糊?一线排查问答实录Navigator_Z12 小时前
LeetCode //C - 1313. Decompress Run-Length Encoded List程序员飞哥12 小时前
AI 编程额度怎么用才不浪费?yh弓长12 小时前
Spring MVC 的请求链路拆房老料12 小时前
【第四部分:Agentic 智能体设计模式】17. Agent 路由模式详解:从意图识别到动态模型选择Sarvartha12 小时前
Iterator 迭代器、ListIterator & foreach 循环