一个菜鸟的算法导论学习笔记【Chapter 2】
2017-04-28 16:24
169 查看
Chapter 2 Getting Started
2.1 Insertion sort
#include<stdio.h> #include<iostream> using namespace std; int a[10]={123,134,3452,234,65,434,234,978,11234,345}; int main()//由小到大排序 { for(int i=0;i<10;i++) { int j=i; int key=a[i]; for(;j>0&&a[j-1]>=key;j--) a[j]=a[j-1]; a[j]=key; } for(int i=0;i<10;i++) cout<<a[i]<<"\t"; return 0; }
2.2 Analyzing algorithms
usually use worst-case analysisoder of growth :
eg:
n^3/1000 -100n^2-100n, the order of order of growth is theta(n^3)
2.3 Designing algorithms
divide and conquerDivide>>>Conquer>>>Combine
eg:
Merge sort
int a[100], b[100]; void Merge(int*a,int p,int q,int r) { int p1=p,p2=q+1,p3=0; while(p1<=q&&p2<=r) b[p3++]=a[p1]<a[p2]?a[p1++]:a[p2++]; while(p1<=q) b[p3++]=a[p1++]; while(p2<=r) b[p3++]=a[p2++]; for(int i=p;i<=r;i++) a[i]=b[i-p]; return; } void MergeSort(int *a,int p,int r)//对数组a,从第p位到第r位由小到大排序 { if(p>=r) return; int q=(p+r)/2; MergeSort(a,p,q); MergeSort(a,q+1,r); Merge(a,p,q,r);//Combine return; }
theta(nln(n))
相关文章推荐
- 一个菜鸟的算法导论学习笔记【Introsuction&Chapter 1】
- 一个菜鸟的算法导论学习笔记【Chapter 4】
- 一个菜鸟的算法导论学习笔记【Chapter 3】
- Introduction to Algorithms (算法导论) 学习笔记 -- Chapter 9
- 算法导论学习笔记之摊还分析
- 中位数和顺序统计量---算法导论学习笔记
- 算法导论学习笔记(11)——贪心算法之哈夫曼树
- 【算法导论学习笔记】最大子数组问题
- 【算法导论】学习笔记——第11章 散列表
- 算法导论学习笔记——基数排序
- 算法导论学习笔记-第五章-概率分析和随机算法
- 算法导论学习笔记-第2章 算法入门
- 算法导论学习笔记-5.4 概率分析和指示器随机变量的应用-几个有趣的问题
- 算法导论----学习笔记08
- 算法导论学习笔记(1)——快排中hoarePartition的实现(问题已解决)
- 算法导论学习笔记-第十四章-数据结构的扩张
- 算法导论学习笔记-1
- 算法导论学习笔记-第九章-中位数和顺序统计学
- 麻省理工算法导论学习笔记(3)----分治法
- 算法导论学习笔记——桶排序