Mit Algorithm tutorial 1

//算法是一个程序员的基础,也是重中之重,我希望能重头系统的学习一遍算法。

Analysis of Algorithm

在程序领域,what‘s more important than the perfermance?

正确性,简洁性,健壮性。features,模块化,security, user friendly。

如果这些都比性能重要,那为什么我们还要学习性能和算法呢?

性能在很多时候,是用户体验的保证,

1,性能的好与坏,可能决定程序的可行于不可行。

2,算法是一种描述程序行为的语言。

3,是用户体验和安全的保障。

4,性能相当于一种货币,是一种标准和基础。

5,算法是一种刺激的东西。

startup with a simple problem

Sorting:

  Input Array <a1, a2, ...,an> of number s.

  Output permutation <a1‘, a2‘, ..., an‘> a1‘ <= a2’ <= ...<= an;

Insertion sort:

  

 1 void InsertionSort(vector<int>& nums, int n)
 2 {
 3     int key;
 4     //插入排序
 5     //每一个pass,在有序数列0 - i-1 中插入第i个元素
 6     for (int i = 1; i < n; ++i)
 7     {
 8         key = nums[i];
 9         for (int j = i - 1; j >= 0; --j)
10         {
11             if (nums[j] >= key)
12             {
13                 //比key大的向前挪一位
14                 nums[j + 1] = nums[j];
15                 if (j == 0) nums[j] = key;
16             }
17             else
18             {
19                 nums[j + 1] = key;
20                 break;
21             }
22
23
24         }
25     }
26 }

issue of the running time:

  1,depend on the input itself.(e.g. already sorted)

  2,depend on the input size.(6 elem VS. 6^9 elem)

  3,Want upper bounds.(guarantee to the users)

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 inputs of size n;

  Best case:(bogus)

    

What is the sort‘s w-c time:

  Depends on your computer;

  relative speed (on same machine)

  absolute speed (on diff machine)

Big Idea:渐进分析

  1,Ignore the machine dependent

  2,look at growth of T(n)    n  -> 无穷

  θ :弃去低阶项,并忽略前面的常熟因子

    当n趋近于无穷大的时候,性能只受到最高项的影响。(可以做到忽略不同计算机上性能的影响,消除特异性的麻烦)

  Insertion sort :T(n) =   ∑(from 2 - n) j = 1 + 2 + 3 + 4 ... = θ(n^2)

  is insertion sort fast?




  Merge sorting: A[1, 2, .. , n]

    1 if n = 1, done;-----------------------------------------------------θ(1)

    2 Recursively sort A[1, 2, .. , [n/2]] and A[[n/2]+1, .., n];-----2θ(n/2)

    3 Merge sorted list--------------------------------------------------θ(n)

  递归

  the height of the tree is lg(n)  the work of each level is Cn, So the total work is Theta(nlgn)

void Merge(vector<int>& nums, int begin, int half, int end)
{
    int n1, n2;
    int *left = NULL, *right = NULL;

    n1 = half - begin + 1;
    n2 = end - half;

    left = (int *)malloc(sizeof(int) * n1);
    right = (int *)malloc(sizeof(int) * n2);

    for (int i = 0; i < n1; i++)
    {
        left[i] = nums[begin + i];

    }
    for (int j = 0; j < n2; j++)
    {
        right[j] = nums[half + 1 + j];
    }

    int k = begin;
    int i = 0, j = 0;
    while (i < n1 && j < n2)
    {
        if (left[i] > right[j])
        {
            nums[k++] = right[j++];
        }
        else
        {
            nums[k++] = left[i++];
        }
    }
    for (; i < n1; i++)
    {
        nums[k++] = left[i];
    }
    for (; j < n2; j++)
    {
        nums[k++] = right[j];
    }
}

void MergeSort(vector<int>& nums, int begin, int end)
{
    int half = (end - begin) / 2;
    if (begin < end){
        MergeSort(nums, begin , begin + half);
        MergeSort(nums, begin + half + 1, end);
        Merge(nums, begin, begin + half, end);
    }
}
时间: 2024-07-29 00:15:32

Mit Algorithm tutorial 1的相关文章

Mit Algorithm 2

1,关于算法的一些符号 O: 表示比它低阶的所有函数集:O(n^3) = n^2; O(g(n))(代表一个函数集) = f(n) f(n ) = O(n) + n^2; O规定了上界 反之, Ω()与O()相反,规定了上届 θ()是O()和Ω()的交集,n^2 + O(n^2) = θ(n^2); o()相当于大于不等于,而w()相当于大于不等于.   2,Solving recurrences 1,替换算法. 1)guess the answer 2)verify the induction

分布式系统(Distributed System)资料

这个资料关于分布式系统资料,作者写的太好了.拿过来以备用 网址:https://github.com/ty4z2008/Qix/blob/master/ds.md 希望转载的朋友,你可以不用联系我.但是一定要保留原文链接,因为这个项目还在继续也在不定期更新.希望看到文章的朋友能够学到更多. <Reconfigurable Distributed Storage for Dynamic Networks> 介绍:这是一篇介绍在动态网络里面实现分布式系统重构的paper.论文的作者(导师)是MIT

MIT算法导论——第一讲.Analysis of algorithm

本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. Leiserson和Erik Demaine老师的讲解.(http://v.163.com/special/opencourse/algorithms.html) 第一节-------课程简介及算法分析 Analysis of algorithm 算法分析:关于计算机程序在效率和资源利用方面的理论

A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning

A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning by Jason Brownlee on September 9, 2016 in XGBoost 0 0 0 0 Gradient boosting is one of the most powerful techniques for building predictive models. In this post you will dis

A great tutorial with Jupyter notebook for ML beginners

An end to end implementation of a Machine Learning pipeline SPANDAN MADAN Visual Computing Group, Harvard University Computer Science and Artificial Intelligence Laboratory, MIT Link to Github Repo Section 1. Introduction Background In the fall of 20

图像分类之特征学习ECCV-2010 Tutorial: Feature Learning for Image Classification

ECCV-2010 Tutorial: Feature Learning for Image Classification Organizers Kai Yu (NEC Laboratories America, [email protected]), Andrew Ng (Stanford University, [email protected]) Place & Time: Creta Maris Hotel, Crete, Greece, 9:00 – 13:00, September

akka---Getting Started Tutorial (Java): First Chapter

原文地址:http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html Introduction Welcome to the first tutorial on how to get started with Akka and Java. We assume that you already know what Akka and Java are and will now focus on the steps

Phalcon之教程 2:INVO 项目讲解(Tutorial 2: Explaining INVO)

教程 2:INVO 项目讲解(Tutorial 2: Explaining INVO) In this second tutorial, we'll explain a more complete application in order to deepen the development with Phalcon. INVO is one of the applications we have created as samples. INVO is a small website that a

How to Configure the Gradient Boosting Algorithm

How to Configure the Gradient Boosting Algorithm by Jason Brownlee on September 12, 2016 in XGBoost 0 0 0 0 Gradient boosting is one of the most powerful techniques for applied machine learning and as such is quickly becoming one of the most popular.