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 && array[i] == array[j]) {
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
相关推荐
froginwe11几秒前
ECharts 旭日图:全面解析与应用指南yaoxin5211233 分钟前
292. Java Stream API - 使用构建器模式创建 Stream@Aurora.7 分钟前
优选算法【专题三:二分查找算法】soldierluo7 分钟前
向量与向量数据CoderCodingNo10 分钟前
【GESP】C++六级考试大纲知识点梳理, (2) 哈夫曼树、完全二叉树与二叉排序树阮松云11 分钟前
code-server 配置maven木木木一15 分钟前
Rust学习记录--C11 编写自动化测试bug总结19 分钟前
uniapp+动态设置顶部导航栏使用详解a努力。23 分钟前
字节跳动Java面试被问:一致性哈希的虚拟节点和数据迁移qq_3181215923 分钟前
互联网大厂Java面试故事:支付与金融服务微服务架构、消息队列与AI风控全流程解析