Introduction to Algorithms Learning Note one: Course Introduction and algorithm analysis

Source: Internet
Author: User

MIT's algorithm introduction Open class, many years ago saw, has not insisted to see, recently looking for summer internship, interview is basically algorithm, had to take time to brush Leetcode, also through this opportunity hope to see this video, the algorithm of the basic skills to play a solid, this public class is still quite good.

Before learning other things, remember a lot of notes, and finally lost, want to look at the time has not been found, so thought to put the study notes on the blog, so that the convenience of their own query.

Public Lesson Video Address: http://open.163.com/special/opencourse/algorithms.html

Section I: Course Introduction and algorithm analysis

The first episode of the video half of the time is to give students the curriculum requirements and homework, the rest of the only talk about the dry, to insertion sort and merge sort as the entry point, explain the algorithm analysis.

Insertion Sort:

Pseudocode

// sorts a[1, ..., n]  for 2 To N      Do Key←a[j]         1         while 0 and A[i] > key            do a[i+1]←a[i]            1          a[i+1]←key

Example:

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4) 8 9 6

2 3 4 6 8 9

Running Time:

    • Depends on input (eg. already sorted or not)
    • Depends on input size (-parameterize in input size)
    • Want upper Bounds

Kinds of analysis

Worst-case (usually)

T (n) = max time on any input of size n

Average-case (sometimes)

T (n) = expected time over all input of size n

(Need assumption of statistic distribution)

Best-case (bogus)

Cheat

What is insertion sort ' s worst time?

Depends on Compater

--relative speed (on same machine)

--absolute speed (on different machine)

BIG idea! --asymptotic Analysis

    1. Ignore Machine-dependent Constants
    2. Look at growth of the running time. T (n) as n→∞

Asymptotic notation

Θ-notation Drop low-order Terms and ignore leading constants

Ex. 3n3+90n2-5n+6046 =θ (N3)

As n→∞,θ (n2) algorithm always beats aθ (n3) algorithm.

Sometimes, n0 May is too big so, a low-speed algorithm would is better.

Insertion Sort Analysis

Worst-case:input Reverse Sorted

T (n) = =θ (n2)

is insertion Sort fast?

    • Moderately so, for small n
    • Not at all for large n

Merge Sort Analysis

Merge sort a[1, ..., n]11, done.       -------------------------- T (n)2. Recursively sort      --------------------------θ (1) a[1, ..., n/2] and a[n/< C12>21, ..., n]   -----2T (n/2)32 sorted list              -----------------θ (n)

Key Subroutine:merge

For example, the sorted list:

2 7 13 20

1 9 11 12

Compare 2 and 1, choose 1 [1]
Comapre 2 and 9, choose 2 [1, 2]
Comapre 7 and 9, choose 7 [1, 2, 7]
Comapre and 9, choose 9 [1, 2, 7, 9]
Comapre and one, choose 11 [1, 2, 7, 9, 11]
Comapre and choose 12 [1, 2, 7, 9, 11, 12]
Put and the end [1, 2, 7, 9, 11, 12, 13, 20]

Time =θ (n) on n totoal elements

Recurrence

T (n) =θ (1), If n = 1 (usually omit it)

T (n) = 2T (N/2) +θ (n)

Recursive Tree for T (n) = 2T (N/2) + CN

The height of this tree is LGN, the number of leavers are N, the subtotal of each depth are CN, so the total time is (CN) LGN +θ (N)

Omit the constant C and low-orderθ (n), T (n) =θ (NLGN).

Introduction to Algorithms Learning Note one: Course Introduction and algorithm analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.