From 30b4e0aad1266d206908d46586962fd4254be5c7 Mon Sep 17 00:00:00 2001
From: EntropyIncreaser <2743134282@qq.com>
Date: Sun, 27 Mar 2016 08:46:01 +0800
Subject: [PATCH 1/5] =?UTF-8?q?=E6=9C=80=E9=95=BF=E4=B8=8A=E5=8D=87?=
=?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
longest_increasing_sequence.md | 10 ++++++++++
longest_increasing_sequence.py | 31 +++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
create mode 100644 longest_increasing_sequence.md
create mode 100644 longest_increasing_sequence.py
diff --git a/longest_increasing_sequence.md b/longest_increasing_sequence.md
new file mode 100644
index 0000000..3f6c47d
--- /dev/null
+++ b/longest_increasing_sequence.md
@@ -0,0 +1,10 @@
+# 问题简介
+最长公共上升子序列,longest increasing sequence,简称LCIS
+# 思路
+这个问题要由动态规划来解决,时间复杂度是O(n2),建立dp[n]和track[n]
+意义:
+----
+* dp[i]:以数据a[i]结尾的最长上升序列长度
+* track[i]:数据a[i]作为最长上升序列末项的前一项索引
+
+
diff --git a/longest_increasing_sequence.py b/longest_increasing_sequence.py
new file mode 100644
index 0000000..a1d3938
--- /dev/null
+++ b/longest_increasing_sequence.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+#coding: utf-8
+
+def isBiggerCompare(a, b):
+ return a > b
+
+def findLIS(sequence, compare = isBiggerCompare):
+ n = len(sequence)
+ dp = [0 for i in range(n)]
+ track = [-1 for i in range(n)]
+ ans = 1
+ for i in range(1, n):
+ MAX = 0
+ for j in range(i):
+ if compare(sequence[i], sequence[j]) and MAX < dp[j]:
+ MAX = dp[j]
+ track[i] = j
+ # track[i] sequence[i]作为最长上升序列末项的前一项
+ dp[i] = MAX + 1
+ # dp[i] 以sequence[i]结尾的最长上升序列长度
+ if dp[i] > dp[ans]:
+ ans = i
+ ansList = [sequence[ans]]
+ while track[ans] != -1:
+ ans = track[ans]
+ ansList.insert(0, sequence[ans])
+ return ansList
+
+if __name__ == '__main__':
+ s = [3, 1, 2, 1, 4, 3, 5]
+ print findLIS(s)
From 6afca92ccfe92c5386a51a6cd7f0cb0de77d63c2 Mon Sep 17 00:00:00 2001
From: EntropyIncreaser <2743134282@qq.com>
Date: Mon, 28 Mar 2016 17:39:38 +0800
Subject: [PATCH 2/5] Update longest_increasing_sequence.md
---
longest_increasing_sequence.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/longest_increasing_sequence.md b/longest_increasing_sequence.md
index 3f6c47d..ee72016 100644
--- a/longest_increasing_sequence.md
+++ b/longest_increasing_sequence.md
@@ -1,7 +1,7 @@
# 问题简介
最长公共上升子序列,longest increasing sequence,简称LCIS
# 思路
-这个问题要由动态规划来解决,时间复杂度是O(n2),建立dp[n]和track[n]
+这个问题要由动态规划来解决,时间复杂度是O(n2),空间复杂度是O(n),建立dp[n]和track[n]
意义:
----
* dp[i]:以数据a[i]结尾的最长上升序列长度
From f04cadadbe05559e4d840ba4b611bfb33aae9949 Mon Sep 17 00:00:00 2001
From: Nailcui
Date: Mon, 15 Aug 2016 14:03:41 +0800
Subject: [PATCH 3/5] Update quick_sort.md
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
#问题
快速排序,这是一个经典的算法,本文给出几种python的写法,供参考。
特别是python能用一句话实现快速排序。
#思路说明
快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)。
(1) 分治法的基本思想
分治法的基本思想是:将原问题分解为若干个规模更小但结构与原问题相似的子问题。递归地解这些子问题,然后将这些子问题的解组合为原问题的解。
(2)快速排序的基本思想
设当前待排序的无序区为R[low..high],利用分治法可将快速排序的基本思想描述为:
###分解:
在R[low..high]中任选一个记录作为基准(Pivot),以此基准将当前无序区划分为左、右两个较小的子区间R[low..pivotpos-1)和R[pivotpos+1..high],并使左边子区间中所有记录的关键字均小于等于基准记录(不妨记为pivot)的关键字pivot.key,右边的子区间中所有记录的关键字均大于等于pivot.key,而基准记录pivot则位于正确的位置(pivotpos)上,它无须参加后续的排序。
注意:
划分的关键是要求出基准记录所在的位置pivotpos。划分的结果可以简单地表示为(注意pivot=R[pivotpos]):
R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys
其中low≤pivotpos≤high。
###求解:
通过递归调用快速排序对左、右子区间R[low..pivotpos-1]和R[pivotpos+1..high]快速排序。
###组合:
因为当"求解"步骤中的两个递归调用结束时,其左、右两个子区间已有序。对快速排序而言,"组合"步骤无须做什么,可看作是空操作。
#解决(Python)
#!/usr/bin/env python
#coding:utf-8
#方法1
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pivot = arr[0] #将第一个值做为基准
for i in arr:
if i < pivot:
less.append(i)
elif i > pivot:
more.append(i)
else:
pivotList.append(i)
less = quickSort(less) #得到第一轮分组之后,继续将分组进行下去。
more = quickSort(more)
return less + pivotList + more
#方法2
# 分为<, >, = 三种情况,如果分为两种情况的话函数调用次数会增加许多,以后几个好像都有相似的问题
# 如果测试1000个100以内的整数,如果分为<, >=两种情况共调用函数1801次,分为<, >, = 三种情况,共调用函数201次
def qsort(L):
return (qsort([y for y in L[1:] if y < L[0]]) + L[:1] + [y for y in L[1:] if y == L[0] + qsort([y for y in L[1:] if y > L[0]])) if len(L) > 1 else L
#方法3
#基本思想同上,只是写法上又有所变化
def qsort(list):
if not list:
return []
else:
pivot = list[0]
less = [x for x in list if x < pivot]
more = [x for x in list[1:] if x >= pivot]
return qsort(less) + [pivot] + qsort(more)
#方法4
from random import *
def qSort(a):
if len(a) <= 1:
return a
else:
q = choice(a) #基准的选择不同于前,是从数组中任意选择一个值做为基准
return qSort([elem for elem in a if elem < q]) + [q] * a.count(q) + qSort([elem for elem in a if elem > q])
#方法5
#这个最狠了,一句话搞定快速排序,瞠目结舌吧。
qs = lambda xs : ( (len(xs) <= 1 and [xs]) or [ qs( [x for x in xs[1:] if x < xs[0]] ) + [xs[0]] + qs( [x for x in xs[1:] if x >= xs[0]] ) ] )[0]
if __name__=="__main__":
a = [4, 65, 2, -31, 0, 99, 83, 782, 1]
print quickSort(a)
print qSort(a)
print qs(a)
---
quick_sort.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/quick_sort.md b/quick_sort.md
index 362a14a..6df98ac 100644
--- a/quick_sort.md
+++ b/quick_sort.md
@@ -65,10 +65,10 @@ R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys
return less + pivotList + more
#方法2
- #将方法1写的更紧凑,彰显python特点
-
+ # 分为<, >, = 三种情况,如果分为两种情况的话函数调用次数会增加许多,以后几个好像都有相似的问题
+ # 如果测试1000个100以内的整数,如果分为<, >=两种情况共调用函数1801次,分为<, >, = 三种情况,共调用函数201次
def qsort(L):
- return (qsort([y for y in L[1:] if y < L[0]]) + L[:1] + qsort([y for y in L[1:] if y >= L[0]])) if len(L) > 1 else L
+ return (qsort([y for y in L[1:] if y < L[0]]) + L[:1] + [y for y in L[1:] if y == L[0] + qsort([y for y in L[1:] if y > L[0]])) if len(L) > 1 else L
#方法3
#基本思想同上,只是写法上又有所变化
From 10b2f7a3ee9af6469d82eccf0d3067329706662b Mon Sep 17 00:00:00 2001
From: Qiaolun Zhang
Date: Fri, 14 Oct 2016 16:31:26 +0800
Subject: [PATCH 4/5] Update quick_sort.py
---
quick_sort.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/quick_sort.py b/quick_sort.py
index 7d35746..83f3b68 100644
--- a/quick_sort.py
+++ b/quick_sort.py
@@ -74,7 +74,7 @@ def qsort(list):
return []
else:
pivot = list[0]
- less = [x for x in list if x < pivot]
+ less = [x for x in list[1:] if x < pivot]
more = [x for x in list[1:] if x >= pivot]
return qsort(less) + [pivot] + qsort(more)
"""
From 40b3637a12e3a52124a3b5baa0ba4f596c17ca9c Mon Sep 17 00:00:00 2001
From: Jiuli Gao
Date: Tue, 22 May 2018 17:03:43 +0800
Subject: [PATCH 5/5] =?UTF-8?q?Markdown=E8=AF=AD=E6=B3=95=E4=BF=AE?=
=?UTF-8?q?=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Markdown语法修正
---
quick_sort.md | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/quick_sort.md b/quick_sort.md
index 6df98ac..1d7c542 100644
--- a/quick_sort.md
+++ b/quick_sort.md
@@ -1,10 +1,10 @@
-#问题
+# 问题
快速排序,这是一个经典的算法,本文给出几种python的写法,供参考。
特别是python能用一句话实现快速排序。
-#思路说明
+# 思路说明
快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)。
@@ -16,7 +16,7 @@
设当前待排序的无序区为R[low..high],利用分治法可将快速排序的基本思想描述为:
-###分解:
+### 分解:
在R[low..high]中任选一个记录作为基准(Pivot),以此基准将当前无序区划分为左、右两个较小的子区间R[low..pivotpos-1)和R[pivotpos+1..high],并使左边子区间中所有记录的关键字均小于等于基准记录(不妨记为pivot)的关键字pivot.key,右边的子区间中所有记录的关键字均大于等于pivot.key,而基准记录pivot则位于正确的位置(pivotpos)上,它无须参加后续的排序。
@@ -28,16 +28,17 @@ R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys
其中low≤pivotpos≤high。
-###求解:
+### 求解:
通过递归调用快速排序对左、右子区间R[low..pivotpos-1]和R[pivotpos+1..high]快速排序。
-###组合:
+### 组合:
因为当"求解"步骤中的两个递归调用结束时,其左、右两个子区间已有序。对快速排序而言,"组合"步骤无须做什么,可看作是空操作。
-#解决(Python)
+### 解决(Python)
+```python
#!/usr/bin/env python
#coding:utf-8
@@ -64,7 +65,7 @@ R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys
return less + pivotList + more
- #方法2
+ # 方法2
# 分为<, >, = 三种情况,如果分为两种情况的话函数调用次数会增加许多,以后几个好像都有相似的问题
# 如果测试1000个100以内的整数,如果分为<, >=两种情况共调用函数1801次,分为<, >, = 三种情况,共调用函数201次
def qsort(L):
@@ -106,3 +107,4 @@ R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys
print qSort(a)
print qs(a)
+```