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
相关推荐
程序喵大人9 分钟前
【C++进阶】STL容器与迭代器 - 01 STL 容器先解决元素放在哪里cui_ruicheng26 分钟前
Python从入门到实战(十六):多进程编程Jelena157795857921 小时前
电商运营分析数据比价接口实战:多平台价格监控与智能决策系统wdfk_prog2 小时前
嵌入式面试真题第 15 题:不可恢复异常后的通用崩溃快照、调用栈保存与离线分析架构晴空了无痕2 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线神明不懂浪漫2 小时前
【第五章】Java中的继承与多态神州世通3 小时前
解密企业通信安全防线:Avaya Aura 三层安全架构深度解析AC赳赳老秦4 小时前
企业工商公开信息采集分析:OpenClaw 批量查询企业工商信息,生成企业画像报告AI多Agent协作实战派4 小时前
AI多Agent协作系统实战(十七):凌晨4点,我的AI系统在“假装工作“——3个bug同时爆炸的5小时