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 小时前
OCR到底是接入第三方API 还是自己封装?kiracrimson12 小时前
STL:List的使用和模拟实现二十七日秋13 小时前
算法刷题笔记「Day 1|四道 OJ 入门题里的深坑」码智社13 小时前
Redis 哨兵模式详解:从原理到生产实践晚风叙码13 小时前
Linux 进程概念(下):程序地址空间与虚拟内存救救孩子把13 小时前
结构化输出为何“格式正确、答案错误”?JSON Schema 的能力边界北冥you鱼13 小时前
Go 语言 GORM 使用 Preload:从关联查询到嵌套预加载的完整实践silvamux_13 小时前
微头条爆款体平台适配实测:把交付要求变成可计算的指标论文复现现场13 小时前
DeepSeek V4.1 Flash 和 V4 Pro 有什么区别?API 价格、性能与迁移建议dingzd9513 小时前
TikTok账号登录态失效的Cookie字段排查路径