Data Structures and Programming Methodology CS61

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 .

相关推荐
咖啡八杯5 小时前
GoF设计模式——策略模式
java·后端·spring·设计模式
To_OC9 小时前
LC 1 两数之和:面试第一道必考题,暴力解法直接被面试官 pass
javascript·算法·leetcode
用户1285261160213 小时前
我把祖传Java项目重构后,接口响应从3s砍到了200ms,只改了这几行代码
java
鱼鱼不愚与13 小时前
《原来如此 | 第01期:为什么导航软件能预测红绿灯倒计时?》
算法
Linsk13 小时前
组件 = 模板 + 业务逻辑
java·前端·vue.js
星沉远浦14 小时前
用Gemini高效解决Java代码报错难以定位的问题
java
用户2986985301418 小时前
Word 文档字符级格式化:Java 实现方案详解
java·后端
复杂网络18 小时前
论最小 Agent 计算机的形态
算法
笨鸟飞不快18 小时前
从单个服务到集群:一次完整的性能排查复盘
java·前端