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
相关推荐
Navigator_Z13 小时前
LeetCode //C - 1326. Minimum Number of Taps to Open to Water a GardenRain的Java大神之路13 小时前
DTO、VO、PO 到底要不要拆?这场 Java 分层之争,今天一次讲透CoderYanger13 小时前
Java EE 进阶:14.案例综合练习-博客系统(第三弹) :用户登录+强制登录CoderYanger19 小时前
Java EE 进阶:13. Spring事务和事务传播机制(第二弹)会编程的吕洞宾1 天前
Spring AI 2.0 MCP 客户端实战:让 Java AI 应用接入现成工具星恒随风1 天前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制geats人山人海1 天前
算法基础第六讲 图论yingjie1101 天前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)Felven1 天前
C. Make Them Equal