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 分钟前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:Dinic最大流解析JasonChen@66638 分钟前
2.Tomcat 安装部署与一键自动化脚本实战开开心心就好39 分钟前
显示器关不掉怎么办,用小工具一键息屏专注仿真1 小时前
基于 AFSIM 的智能体训练:多智能体强化学习篇 —— QTRAN 算法实战FR02EN7771 小时前
[LeetCode] 26、80.删除有序数组中的重复项 题解SimonKing1 小时前
零 OCR 依赖!Firecrawl 这个 Rust 库把 Word / PPT / Excel 全变成 MarkdownRain的Java大神之路1 小时前
别被 ChatGPT 的打字机效果骗了:一字一字蹦,才是 LLM 自回归的命门(附大厂 Java 流式落地)渣渣盟2 小时前
狂命爆肝99天,共52K字的JAVA学习笔记奉上,JAVA从入门到精通一文搞定,一文在手JAVA无忧Funlang3 小时前
RISC-V 上的 Fun:第四套机器码星落梦生3 小时前
Maven高级(9)