算法学习笔记--2. Selection sort & Fibonacci sequence
2017-08-07 23:42
316 查看
书中关于算法和运行时间的介绍:
Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.
The running time of an algorithm on a particular input is the number of primitive operations or “steps” executed.
L.sort()时灵时不灵的,不知道为什么。
这里可以自己写排序方法。
比如,最简单的这个选择排序法。
(比较两个元素,如果他们的顺序错误,就把他们交换过来。)
注意python中列表索引的第一个数字是0,斐波那契数列从1开始。
这里最主要的是应用了递归的方法,在python函数中使用了函数本身。
Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.
The running time of an algorithm on a particular input is the number of primitive operations or “steps” executed.
1. 排序
python自带的有列表排序方法,sorted(L)方法可以用。L.sort()时灵时不灵的,不知道为什么。
>>> help(list.sort) Help on method_descriptor: sort(...) L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* >>> >>> help(sorted) Help on built-in function sorted in module builtins: sorted(iterable, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
这里可以自己写排序方法。
比如,最简单的这个选择排序法。
(比较两个元素,如果他们的顺序错误,就把他们交换过来。)
def sel_sort(lists): for i in range(len(lists)): for j in range(i + 1, len(lists)): if lists[i] > lists[j]: lists[i], lists[j] = lists[j], lists[i] return lists >>> L = [2, 5, 98, 56, 3, 0] >>> sel_sort(L) [0, 2, 3, 5, 56, 98] >>>
2. 斐波那契数列
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)注意python中列表索引的第一个数字是0,斐波那契数列从1开始。
这里最主要的是应用了递归的方法,在python函数中使用了函数本身。
def fib(n): """Assumes n an int > 0, returns fibonacci of n""" if n == 1 or n == 2: return 1 else: return fib(n-1)+fib(n-2) >>> fib(3) 2 >>> fib(6) 8 >>> fib(9) 34 >>>
相关文章推荐
- 算法学习笔记--5.merge sort & divide-and-conquer
- [算法Tutorial]Sort & Selection in O(n)
- <算法导论>学习笔记(3)--递归树求递归算法时间复杂度
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】
- 数据结构&算法学习笔记: 快速排序
- PHP简单选择排序(Simple Selection Sort)算法学习
- C++ Primer 学习笔记:泛型算法入门 fill && back_inserter && copy && replace
- 算法学习笔记之——priority queue、heapsort、symbol table、binary search trees
- 算法学习笔记 —— MergeSort 的一种循环实现
- 『算法学习笔记』9th day. 循环结构 & 计时器函数的使用
- 算法学习笔记一---如何进行算法分析&渐近符号介绍
- 回归&logistics回归笔记-七月算法(julyedu.com)4 月机器学习算法班学习笔记
- 学习笔记之快速排序——quicklySort——基础算法——java
- C++ Primer 学习笔记:泛型算法入门 插入迭代器 && iostream迭代器 && 反向迭代器
- 数据结构&算法学习笔记: 归并排序
- 『算法学习笔记』4th -8th day. 10道习题 & 常用数学函数
- [学习笔记—算法] 正序 & 倒序 提取整数各位上的数字
- C++学习笔记26——泛型算法之容器元素排序(sort unique)
- 算法学习----->Insertsort(插入排序)
- C++ Primer 学习笔记:泛型算法入门 find && accumulate && equal