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
相关推荐
天若有情6736 小时前
程序员原创|借鉴JS事件冒泡,根治电脑文件混乱的“冒泡整理法”一切皆是因缘际会6 小时前
从概率拟合到内生心智:2026 下一代 AI 架构演进与落地实践Java成神之路-6 小时前
【LeetCode 刷题笔记】34. 在排序数组中查找元素的第一个和最后一个位置 | 二分查找经典刷题题解qq_589568106 小时前
springbootweb案例,出现访问 http://localhost:8080/list 一直处于浏览器运转阶段不忘不弃6 小时前
用BFS方法求解平分汽油问题AI科技星6 小时前
全域数学·72分册·射影原本 无穷维射影几何卷细化子目录【乖乖数学】风落无尘6 小时前
《智能重生:从垃圾堆到AI工程师》——第四章 变化的艺术JAVA面经实录9176 小时前
计算机基础(完整版·超详细可背诵)特种加菲猫7 小时前
继承,一场跨越时空的对话AC赳赳老秦7 小时前
知识产权辅助:用 OpenClaw 批量生成专利交底书 / 软著申请材料,自动校验格式与内容合规性