Mit Algorithm tutorial 1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}

 

以上是关于Mit Algorithm tutorial 1的主要内容,如果未能解决你的问题,请参考以下文章

Microsoft Azure Tutorial: Build your first movie inventory web app with just a few lines of code(示例代

论文阅读Parallel multi-objective artificial bee colony algorithm for software requirement optimization(代

26 Java GC算法 垃圾收集器标记 -清除算法复制算法标记-压缩算法分代收集算法

back propogation 的线代描述

Codeforces #480 Tutorial

博客开通啦~