From f30a476aae4b676b63191e68a2082ead68c44f87 Mon Sep 17 00:00:00 2001 From: delronkung Date: Sun, 12 Feb 2017 22:45:07 +0800 Subject: [PATCH] add chapter0 --- algorithm/.ftpconfig | 17 --------------- algorithm/README.md | 38 ++++++++++++++-------------------- algorithm/SUMMARY.md | 8 +++++-- algorithm/chapter0/README.md | 5 +++++ algorithm/chapter0/section1.md | 30 +++++++++++++++++++++++++++ algorithm/chapter0/section2.md | 19 +++++++++++++++++ algorithm/chapter0/section3.md | 30 +++++++++++++++++++++++++++ algorithm/chapter0/section4.md | 15 ++++++++++++++ 8 files changed, 120 insertions(+), 42 deletions(-) delete mode 100644 algorithm/.ftpconfig create mode 100644 algorithm/chapter0/README.md create mode 100644 algorithm/chapter0/section1.md create mode 100644 algorithm/chapter0/section2.md create mode 100644 algorithm/chapter0/section3.md create mode 100644 algorithm/chapter0/section4.md diff --git a/algorithm/.ftpconfig b/algorithm/.ftpconfig deleted file mode 100644 index 9aaa9c0..0000000 --- a/algorithm/.ftpconfig +++ /dev/null @@ -1,17 +0,0 @@ -{ - "protocol": "sftp", - "host": "192.168.11.128", // string - Hostname or IP address of the server. Default: 'localhost' - "port": 22, // integer - Port number of the server. Default: 22 - "user": "python", // string - Username for authentication. Default: (none) - "pass": "chuanzhi", // string - Password for password-based user authentication. Default: (none) - "promptForPass": false, // boolean - Set to true for enable password/passphrase dialog. This will prevent from using cleartext password/passphrase in this config. Default: false - "remote": "/home/python/workspace/algorithm", // try to use absolute paths starting with - "agent": "", // string - Path to ssh-agent's UNIX socket for ssh-agent-based user authentication. Windows users: set to 'pageant' for authenticating with Pageant or (actual) path to a cygwin "UNIX socket." Default: (none) - "privatekey": "", // string - Path to the private key file (in OpenSSH format). Default: (none) - "passphrase": "", // string - For an encrypted private key, this is the passphrase used to decrypt it. Default: (none) - "hosthash": "", // string - 'md5' or 'sha1'. The host's key is hashed using this method and passed to the hostVerifier function. Default: (none) - "ignorehost": true, - "connTimeout": 10000, // integer - How long (in milliseconds) to wait for the SSH handshake to complete. Default: 10000 - "keepalive": 10000, // integer - How often (in milliseconds) to send SSH-level keepalive packets to the server (in a similar way as OpenSSH's ServerAliveInterval config option). Set to 0 to disable. Default: 10000 - "watch":[] -} diff --git a/algorithm/README.md b/algorithm/README.md index c382942..eb56bd7 100644 --- a/algorithm/README.md +++ b/algorithm/README.md @@ -1,14 +1,20 @@ -#python数据结构与算法 -> 学了栈之后,你就知道,很多涉及后入先出的问题,例如函数递归就是个栈模型、Android的屏幕跳转就用到栈, -很多类似的东西,你就会第一时间想到:我会用这东西来去写算法实现这个功能。 -学了队列之后,你就知道,对于先入先出要排队的问题,你就要用到队列,例如多个网络下载任务, -我该怎么去调度它们去获得网络资源呢?再例如操作系统的进程(or线程)调度,我该怎么去分配资源(像CPU)给多个任务呢? -肯定不能全部一起拥有的,资源只有一个,那就要排队!那么怎么排队呢?用普通的队列?但是对于那些优先级高的线程怎么办? -那也太共产主义了吧,这时,你就会想到了优先队列,优先队列怎么实现?用堆,然后你就有疑问了,堆是啥玩意? -学了顺序表和链表,你就知道,在查询操作更多的程序中,你应该用顺序表;而修改操作更多的程序中, -你要使用链表;而单向链表不方便怎么办,每次都从头到尾好麻烦啊,怎么办?你这时就会想到双向链表or循环链表。 +# 数据结构与算法(Python) +## Why? +我们举一个可能不太恰当的例子: + +如果将最终写好运行的程序比作战场,我们码农便是指挥作战的将军,而我们所写的代码便是士兵和武器。 + +**那么数据结构和算法是什么?答曰:兵法!** + +我们可以不看兵法在战场上肉搏,如此,可能会胜利,可能会失败。即使胜利,可能也会付出巨大的代价。我们写程序亦然:没有看过数据结构和算法,有时面对问题可能会没有任何思路,不知如何下手去解决;大部分时间可能解决了问题,可是对程序运行的效率和开销没有意识,性能低下;有时会借助别人开发的利器暂时解决了问题,可是遇到性能瓶颈的时候,又不知该如何进行针对性的优化。 + +如果我们常看兵法,便可做到胸有成竹,有时会事半功倍!同样,如果我们常看数据结构与算法,我们写程序时也能游刃有余、明察秋毫,遇到问题时亦能入木三分、迎刃而解。 + +**故,数据结构和算法是一名程序开发人员的必备基本功,不是一朝一夕就能练成绝世高手的。冰冻三尺非一日之寒,需要我们平时不断的主动去学习积累。** + +**通过三天的学习,我们希望让大家能理解其概念,掌握常用的数据结构和算法。** ##数据结构 @@ -38,10 +44,6 @@ 最常用的数据运算有五种: 插入、删除、修改、查找、排序 -##算法 -算法是独立存在的一种解决问题的方法和思想。 -对于算法而言,语言并不重要,重要的是思想。 - **算法与数据结构的区别** @@ -51,13 +53,3 @@ 总结: 算法是为了解决实际问题而设计的 数据结构是算法需要处理的问题载体 - - - -**算法的五大特性** - -输入: 算法具有0个或多个输入 -输出: 算法至少有1个或多个输出 -有穷性: 算法在有限的步骤之后会自动结束而不会无限循环,并且每一个步骤可以在接收的实际内完成 -确定性:算法中的每一步都有确定的含义,不会出现二义性 -可行性:算法的每一步都是可行的,也就是说每一步都能够执行有限的次数完成 diff --git a/algorithm/SUMMARY.md b/algorithm/SUMMARY.md index 250cb74..1eb85e1 100644 --- a/algorithm/SUMMARY.md +++ b/algorithm/SUMMARY.md @@ -1,6 +1,11 @@ # Summary -* [python数据结构与算法简介](README.md) +* [数据结构与算法(Python)](README.md) +* [1 引入概念](chapter0/README.md) + * [第一次尝试](chapter0/section1.md) + * [算法的提出](chapter0/section2.md) + * [第二次尝试](chapter0/section3.md) + * [算法效率衡量](chapter0/section4.md) * [常用数据类型与算法效率分析](chapter1/README.md) * [抽象数据类型](chapter1/section1.md) * [算法效率衡量](chapter1/section2.md) @@ -27,4 +32,3 @@ * [树与树算法](chapter6/README.md) * [二叉树](chapter6/section1.md) * [二叉树的遍历](chapter6/section2.md) - diff --git a/algorithm/chapter0/README.md b/algorithm/chapter0/README.md new file mode 100644 index 0000000..b78eec6 --- /dev/null +++ b/algorithm/chapter0/README.md @@ -0,0 +1,5 @@ +# 1 引入 + +先来看一道题: + +**如果 a+b+c=1000,且 a^2+b^2=c^2(a,b,c 为自然数),如何求出所有a、b、c可能的组合?** diff --git a/algorithm/chapter0/section1.md b/algorithm/chapter0/section1.md new file mode 100644 index 0000000..a341c60 --- /dev/null +++ b/algorithm/chapter0/section1.md @@ -0,0 +1,30 @@ +# 第一次尝试 + +```python +import time + +start_time = time.time() + +# 注意是三重循环 +for a in range(0, 1001): + for b in range(0, 1001): + for c in range(0, 1001): + if a**2 + b**2 == c**2 and a+b+c == 1000: + print("a, b, c: %d, %d, %d" % (a, b, c)) + +end_time = time.time() +print("elapsed: %f" % (end_time - start_time)) +print("complete!") +``` + +运行结果: +``` +a, b, c: 0, 500, 500 +a, b, c: 200, 375, 425 +a, b, c: 375, 200, 425 +a, b, c: 500, 0, 500 +elapsed: 214.583347 +complete! +``` + +**注意运行的时间:214.583347秒** diff --git a/algorithm/chapter0/section2.md b/algorithm/chapter0/section2.md new file mode 100644 index 0000000..5cb749a --- /dev/null +++ b/algorithm/chapter0/section2.md @@ -0,0 +1,19 @@ +# 算法的提出 + +## 算法的概念 + +算法是计算机处理信息的本质,因为计算机程序本质上是一个算法来告诉计算机确切的步骤来执行一个指定的任务。一般地,当算法在处理信息时,会从输入设备或数据的存储地址读取数据,把结果写入输出设备或某个存储地址供以后再调用。 + +**算法是独立存在的一种解决问题的方法和思想。** + +对于算法而言,实现的语言并不重要,重要的是思想。 + +算法可以有不同的语言描述实现版本(如C描述、C++描述、Python描述等),我们现在是在用Python语言进行描述实现。 + +## 算法的五大特性 + +1. **输入**: 算法具有0个或多个输入 +2. **输出**: 算法至少有1个或多个输出 +3. **有穷性**: 算法在有限的步骤之后会自动结束而不会无限循环,并且每一个步骤可以在接收的实际内完成 +4. **确定性**:算法中的每一步都有确定的含义,不会出现二义性 +5. **可行性**:算法的每一步都是可行的,也就是说每一步都能够执行有限的次数完成 diff --git a/algorithm/chapter0/section3.md b/algorithm/chapter0/section3.md new file mode 100644 index 0000000..31cdcb3 --- /dev/null +++ b/algorithm/chapter0/section3.md @@ -0,0 +1,30 @@ +# 第二次尝试 + +```python +import time + +start_time = time.time() + +# 注意是两重循环 +for a in range(0, 1001): + for b in range(0, 1001-a): + c = 1000 - a - b + if a**2 + b**2 == c**2: + print("a, b, c: %d, %d, %d" % (a, b, c)) + +end_time = time.time() +print("elapsed: %f" % (end_time - start_time)) +print("complete!") +``` + +运行结果: +``` +a, b, c: 0, 500, 500 +a, b, c: 200, 375, 425 +a, b, c: 375, 200, 425 +a, b, c: 500, 0, 500 +elapsed: 0.182897 +complete! +``` + +**注意运行的时间:0.182897秒** diff --git a/algorithm/chapter0/section4.md b/algorithm/chapter0/section4.md new file mode 100644 index 0000000..a3f8520 --- /dev/null +++ b/algorithm/chapter0/section4.md @@ -0,0 +1,15 @@ +# 算法效率衡量 + +## 执行时间反应算法效率 + +对于同一问题,我们给出了两种解决算法,在两种算法的实现中,我们对程序执行的时间进行了测算,发现两段程序执行的时间相差悬殊(214.583347秒相比于0.182897秒),由此我们可以得出结论:**实现算法程序的执行时间可以反应出算法的效率,即算法的优劣。** + +## 单靠时间值绝对可信吗? + +假设我们将第二次尝试的算法程序运行在一台配置古老性能低下的计算机中,情况会如何?很可能运行的时间并不会比在我们的电脑中运行算法一的214.583347秒快多少。 + +**单纯依靠运行的时间来比较算法的优劣并不一定是客观准确的!** + +程序的运行离不开计算机环境(包括硬件和操作系统),这些客观原因会影响程序运行的速度并反应在程序的执行时间上。那么如何才能客观的评判一个算法的优劣呢? + +## 时间复杂度与“大O记法”