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
相关推荐
AAA修煤气灶刘哥10 分钟前
QQ 音乐怎么在 25 毫秒内,决定一句话该交给谁?SimonKing27 分钟前
Typora 收费,开源产品Textus或可替代,还支持AI泡海椒39 分钟前
Java 生成资金流向图 PDF:jquick-pdf 树图(JTreeChartData)节点尺寸与间距的追问记录Rain的Java大神之路43 分钟前
别再死背八股了:读完 Spring 7.0.8 源码,我学会了"改"框架旖旎夜光1 小时前
【C 语言】(二)模拟实现 strlen、strcpy、strcmp、strstr、memcpy、memmove开开心心就好1 小时前
电脑禁止自动休眠工具,还能按负载定时codeejun2 小时前
每日一Go·MySQL-22、幻读、不可重复读、脏读实战演示云草桑2 小时前
C#入坑JAVA 潜规则 API Get 多参数 Spring Boot与Spring Cloud 区别 入门系列4码途行者10 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么头发还在的女程序员11 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同