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
相关推荐
Free Tester6 小时前
如何判断 LeakCanary 报告的严重程度炘爚6 小时前
C++ 右值引用与程序优化si莉亚7 小时前
ROS2安装EVO工具包清心歌7 小时前
CopyOnWriteArrayList 实现原理zyq99101_17 小时前
DFS算法实战:经典例题代码解析智者知已应修善业7 小时前
【51单片机单按键切换广告屏】2023-5-17广州灵眸科技有限公司7 小时前
为RK3588注入澎湃算力:RK1820 AI加速卡完整适配与评测指南qinian_ztc7 小时前
frida 14.2.18 安装报错解决Java成神之路-7 小时前
通俗易懂理解 Spring MVC 拦截器:概念、流程与简单实现(Spring系列16)zhanghongbin017 小时前
AI 采集器:Claude Code、OpenAI、LiteLLM 监控