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
相关推荐
鸽鸽程序猿1 分钟前
【项目】【在线判题系统】b端题目增删改Lhappy嘻嘻6 分钟前
读懂 JVM:内存模型、类加载机制、分代回收黄俊懿6 分钟前
【架构师从入门到进阶】第六章:服务集群优化——第八节:线程数公式Rain的Java大神之路12 分钟前
别再死磕 MySQL 了:向量数据库与 RAG 生产落地实录(Java 工程师专场)七夜zippoe13 分钟前
Spring AI 是什么?Java 工程师的 AI 开发第一站2501_9269783316 分钟前
人机通信协议--人和机器之间的交互想象行者全栈架构师19 分钟前
【鸿蒙心迹】互动卡片全链路——把核心功能放到桌面(FormKit 实战)(HarmonyOS 7.x)是真的小外套24 分钟前
2023年软考中级信息安全工程师|选择题考点+5道案例分析真题解析(含答案)无定义_30 分钟前
10.11日常训练Code_Solitude31 分钟前
c语言:关于qsoet函数知识点