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
相关推荐
坚定学代码14 分钟前
static在c++中的使用hansang_IR27 分钟前
【题解】P5364 [SNOI2017] 礼物Tisfy32 分钟前
LeetCode 3871.统计范围内的逗号 II:每次算一个逗号小O的算法实验室36 分钟前
IEEE TEVC,基于K-means与DQN辅助元启发式算法的多目标两栖无人艇调度模型Json____37 分钟前
从零构建家政服务平台:一套全栈架构如何打通管理端与移动端-java-springbootmmmmath_31 小时前
LeetCode.350.两个数组的交集IIIvanqhz1 小时前
图是“拓扑 + 类型 + 形状“的世界云雀衔光1 小时前
Java Spring AI MCP:在 Spring 里把 AI 接上你的业务系统重生之小比特1 小时前
【Java SE】数据类型与变量苏渡苇1 小时前
Spring Insight 里如何对 Span 进行清洗