Algorithm(Robert Sedgewick)-Day02 | Analysis of Algorithms

Primary pratical reason: avoid performance bugs

Predict performance
Compare algorithms
Provide guarantees
Understand theoretical basis

Use sicentific method to understand performance
Observe some feature of the natural world, generally with precise measurements.
Hypothesize a model that is consistent with the observations.
Predict events using the hypothesis.
Verify the predictions by making further observations.
Validate by repeating until the hypothesis and observations agree.

Principles
experiments: reproducible
hypotheses: falsifiable

Observations
order of growth

descriptionfuction
constant1
logarithmiclogN
linearN
linearithmicNlogN
quadraticN^2
cubicN^3
exponetial2^N

3-Sum problem:
Given N distinct integers, how many triples sum to exactly zero?

brute-force algorithm: N^3
T(N ) = aN^b

int N = a.length;
int count  = 0;
for (int i = 0; i < N; i++)
	for (int j = i + 1; j < N; j++)
		for (int k = j + 1; k < N; k++)
			if (a[i] + a[j] + a[k] == 0)
				count ++;
return count;

Binary search: lgN

int lo = 0, hi = a.length - 1
while(lo <= hi)
{
	int mid = lo + (hi - lo) / 2;
	if 			(key < a[mid]) hi = mid - 1;
	else if (key > a[mid]) lo = mid + 1;
	else return mid;
}

An N^2logN algorithm for 3-Sum:

Sort the N(distinct) numbsers: N^2
Binary search for -(a[i] + a[j]): N^2logN

Analyses

Best case. Lower bound on cost

Worst case. Upper bound on cost

Average case. Expected cost for random input

Commonly-used notations in the theory of algorithms
Big Theta
Big Oh
Big Omega

Memory

typebytes
boolean1
byte1
char2
int4
float4
long8
double8
char[]2N+24
int[]4N+24
double[]8N+24
char[][]2MN
int[][]4MN
double[][]8MN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This fourth edition of Robert Sedgewick and Kevin Wayne’s Algorithms is the leading textbook on algorithms today and is widely used in colleges and universities worldwide. This book surveys the most important computer algorithms currently in use and provides a full treatment of data structures and algorithms for sorting, searching, graph processing, and string processing--including fifty algorithms every programmer should know. In this edition, new Java implementations are written in an accessible modular programming style, where all of the code is exposed to the reader and ready to use. The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable, not just for professional programmers and computer science students but for any student with interests in science, mathematics, and engineering, not to mention students who use computation in the liberal arts. The companion web site, algs4.cs.princeton.edu, contains An online synopsis Full Java implementations Test data Exercises and answers Dynamic visualizations Lecture slides Programming assignments with checklists Links to related material The MOOC related to this book is accessible via the "Online Course" link at algs4.cs.princeton.edu. The course offers more than 100 video lecture segments that are integrated with the text, extensive online assessments, and the large-scale discussion forums that have proven so valuable. Offered each fall and spring, this course regularly attracts tens of thousands of registrants. Robert Sedgewick and Kevin Wayne are developing a modern approach to disseminating knowledge that fully embraces technology, enabling people all around the world to discover new ways of learning and teaching. By integrating their textbook, online content, and MOOC, all at the state of the art, they have built a unique resource that greatly expands the breadth and depth of the educational experience.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值