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 .

相关推荐
java资料站3 小时前
十一、评估测试
开发语言·人工智能·python
左手の明天4 小时前
SLIP 协议封装与解析详解:原理、C 代码与实战案例
linux·c语言·开发语言·c++
shuoshuohaohao4 小时前
MyBatis 小知识点
java·spring·mybatis
海上小飞龙4 小时前
【KMP算法-下篇】同一道题:Java 库函数 2600 微秒,手写 KMP 16 微秒
java·开发语言·算法
青少儿编程课堂5 小时前
后缀自动机解析:本质不同子串与最长重复片段统计
c++·python·算法·bfs·信息学竞赛
朝朝辞暮i5 小时前
C++ 第 12 课:局部变量、作用域、变量生命周期
开发语言·c++·算法
狗凯之家源码网5 小时前
淘客口令落地页生成器 PHP 版深度评测
开发语言·php
画绛集美术5 小时前
本地化部署LLM用于美术史问答:一间美术教室的实践记录
开发语言·php
谢亮_vipxieliang5 小时前
泛型擦除、通配符与 PECS 一次讲透
java·面试
八解毒剂5 小时前
【计组】中央处理器CPU
java·开发语言·计算机组成原理