From 98361c42d75995bb2ae4e5d5bf731dcc0436613a Mon Sep 17 00:00:00 2001 From: duke <276877855@qq.com> Date: Fri, 13 Jul 2018 14:39:20 +0800 Subject: [PATCH 1/6] Delete README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 9d0a5ea..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# hello-world -just a example From 7148b1f22fc8f170a958dd66f0f6142137642c8f Mon Sep 17 00:00:00 2001 From: duke <276877855@qq.com> Date: Fri, 13 Jul 2018 14:40:53 +0800 Subject: [PATCH 2/6] Add files via upload --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..46a9d29 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Algorithm + +* 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。
+ +* [我的个人网站](http://cuijiahua.com/ "悬停显示")
+ +* 学习交流群【328127489】Coder
+ +* 算法学习,持续更新中!
+ +## 八大排序算法 + +* Sort Algorithm.cpp - C++版
+ +* Sort Algorithm.py - Python版
+ +* [程序员内功:八大排序算法](http://cuijiahua.com/blog/2018/01/alogrithm_9.html "悬停显示")
+ +### 原理说明: + +* [排序(1):冒泡排序](http://cuijiahua.com/blog/2017/12/algorithm_1.html "悬停显示")
+ +* [排序(2):直接插入排序](http://cuijiahua.com/blog/2017/12/algorithm_2.html "悬停显示")
+ +* [排序(3):希尔排序](http://cuijiahua.com/blog/2017/12/algorithm_3.html "悬停显示")
+ +* [排序(4):快速排序](http://cuijiahua.com/blog/2017/12/algorithm_4.html "悬停显示")
+ +* [排序(5):简单选择排序](http://cuijiahua.com/blog/2017/12/algorithm_5.html "悬停显示")
+ +* [排序(6):堆排序](http://cuijiahua.com/blog/2018/01/algorithm_6.html "悬停显示")
+ +* [排序(7):归并排序](http://cuijiahua.com/blog/2018/01/algorithm_7.html "悬停显示")
+ +* [排序(8):基数排序](http://cuijiahua.com/blog/2018/01/algorithm_8.html "悬停显示")
From 21f10d8ce403d7c52b8a8e97af373e004d5f051d Mon Sep 17 00:00:00 2001 From: duke <276877855@qq.com> Date: Fri, 13 Jul 2018 14:41:18 +0800 Subject: [PATCH 3/6] Add files via upload From db05b263ba641890db5535f38b02072b9f7b5c89 Mon Sep 17 00:00:00 2001 From: duke <276877855@qq.com> Date: Fri, 13 Jul 2018 14:42:05 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=85=AB=E7=A7=8D=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E7=AE=97=E6=B3=95,python=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sort Algorithms.py | 385 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 385 insertions(+) create mode 100644 Sort Algorithms.py diff --git a/Sort Algorithms.py b/Sort Algorithms.py new file mode 100644 index 0000000..cca9b03 --- /dev/null +++ b/Sort Algorithms.py @@ -0,0 +1,385 @@ +# -*- coding:utf-8 -*- + +def bubbleSort(input_list): + ''' + 函数说明:冒泡排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + if len(input_list) == 0: + return [] + sorted_list = input_list + for i in range(len(sorted_list) - 1): + bChanged = False + print('第%d趟排序:' % (i + 1)) + for j in range(len(sorted_list) - 1): + if sorted_list[j + 1] < sorted_list[j]: + sorted_list[j], sorted_list[j + 1] = sorted_list[j + 1], sorted_list[j] + bChanged = True + print(sorted_list) + if not bChanged: + break + return sorted_list + +def insertSort(input_list): + ''' + 函数说明:直接插入排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + if len(input_list) == 0: + return [] + sorted_list = input_list + + for i in range(1, len(sorted_list)): + temp = sorted_list[i] + j = i - 1 + while j >=0 and temp < sorted_list[j]: + sorted_list[j + 1] = sorted_list[j] + j -= 1 + sorted_list[j + 1] = temp + return sorted_list + + + +def BinaryInsertSort(input_list): + ''' + 函数说明:二分查找插入排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + if len(input_list) == 0: + return [] + + def BinarySearch(input_list, end, value): + ''' + 函数说明:二分查找,查找第一个大于等于value的列表索引值,不存在返回-1 + Website: + http://cuijiahua.com + Parameters: + input_list - 待查找列表 + end - 查找位置的尾部 + value - + Returns: + left - 找到的列表索引值 + ''' + left = 0 + right = end - 1 + while left <= right: + middle = left + (right - left) // 2 + if input_list[middle] >= value: + right = middle - 1 + else: + left = middle + 1 + + return left if left < end else -1 + + sorted_list = input_list + for i in range(1, len(input_list)): + j = i - 1 + temp = sorted_list[i] + insert_index = BinarySearch(sorted_list, i, sorted_list[i]) + if insert_index != -1: + while j >= insert_index: + sorted_list[j + 1] = sorted_list[j] + j -= 1 + sorted_list[j + 1] = temp + return sorted_list + +def ShellSort(input_list): + ''' + 函数说明:希尔排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + if len(input_list) == 0: + return [] + sorted_list = input_list + n = len(sorted_list) + gap = n // 2 + while gap > 0: + for i in range(gap, len(sorted_list)): + temp = sorted_list[i] + j = i - gap + while j >= 0 and temp < sorted_list[j]: + sorted_list[j + gap] = sorted_list[j] + j -= gap + sorted_list[j + gap] = temp + gap //= 2 + return sorted_list + +def QuickSort(input_list, left, right): + ''' + 函数说明:快速排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + 无 + ''' + def division(input_list, left, right): + ''' + 函数说明:根据left和right进行一次扫描,重新找到基准数 + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + left - 左指针位置 + right - 右指针位置 + Returns: + left - 新的基准数位置 + ''' + base = input_list[left] + while left < right: + while left < right and input_list[right] >= base: + right -= 1 + input_list[left] = input_list[right] + while left < right and input_list[left] <= base: + left += 1 + input_list[right] = input_list[left] + input_list[left] = base + return left + + if left < right: + base_index = division(input_list, left, right) + QuickSort(input_list, left, base_index - 1) + QuickSort(input_list, base_index + 1, right) + +def SelectSort(input_list): + ''' + 函数说明:简单选择排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + if len(input_list) == 0: + return [] + sorted_list = input_list + length = len(sorted_list) + for i in range(length): + min_index = i + for j in range(i + 1, length): + if sorted_list[min_index] > sorted_list[j]: + min_index = j + temp = sorted_list[i] + sorted_list[i] = sorted_list[min_index] + sorted_list[min_index] = temp + return sorted_list + +def HeadSort(input_list): + ''' + 函数说明:堆排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + def HeadAdjust(input_list, parent, length): + ''' + 函数说明:堆调整,调整为最大堆 + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + parent - 堆的父结点 + length - 数组长度 + Returns: + 无 + ''' + temp = input_list[parent] + child = 2 * parent + 1 + + while child < length: + if child + 1 < length and input_list[child] < input_list[child+1]: + child += 1 + if temp >= input_list[child]: + break + + input_list[parent] = input_list[child] + + parent = child + child = 2 * parent + 1 + input_list[parent] = temp + + if len(input_list) == 0: + return [] + sorted_list = input_list + length = len(sorted_list) + + for i in range(0, length // 2 + 1)[::-1]: + HeadAdjust(sorted_list, i, length) + + for j in range(1, length)[::-1]: + temp = sorted_list[j] + sorted_list[j] = sorted_list[0] + sorted_list[0] = temp + + HeadAdjust(sorted_list, 0, j) + print('第%d趟排序:' % (length - j), end = '') + print(sorted_list) + + return sorted_list + +def MergeSort(input_list): + ''' + 函数说明:归并排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + def merge(input_list, left, mid, right, temp): + ''' + 函数说明:合并函数 + Website: + http://cuijiahua.com + Parameters: + input_list - 待合并列表 + left - 左指针 + right - 右指针 + temp - 临时列表 + Returns: + 无 + ''' + i = left + j = mid + 1 + k = 0 + + while i <= mid and j <= right: + if input_list[i] <= input_list[j]: + temp[k] = input_list[i] + i += 1 + else: + temp[k] = input_list[j] + j += 1 + k += 1 + + while i <= mid: + temp[k] = input_list[i] + i += 1 + k += 1 + while j <= right: + temp[k] = input_list[j] + j += 1 + k += 1 + + k = 0 + while left <= right: + input_list[left] = temp[k] + left += 1 + k += 1 + + def merge_sort(input_list, left, right, temp): + if left >= right: + return; + mid = (right + left) // 2 + merge_sort(input_list, left, mid, temp) + merge_sort(input_list, mid + 1, right, temp) + + merge(input_list, left, mid, right, temp) + + if len(input_list) == 0: + return [] + sorted_list = input_list + temp = [0] * len(sorted_list) + merge_sort(sorted_list, 0, len(sorted_list) - 1, temp) + return sorted_list + +def RadixSort(input_list): + ''' + 函数说明:基数排序(升序) + Website: + http://cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + sorted_list - 升序排序好的列表 + ''' + def MaxBit(input_list): + ''' + 函数说明:求出数组中最大数的位数的函数 + Author: + www.cuijiahua.com + Parameters: + input_list - 待排序列表 + Returns: + bits-num - 位数 + ''' + max_data = max(input_list) + bits_num = 0 + while max_data: + bits_num += 1 + max_data //= 10 + return bits_num + + def digit(num, d): + ''' + 函数说明:取数xxx上的第d位数字 + Website: + http://cuijiahua.com + Parameters: + num - 待操作的数 + d - 第d位的数 + Returns: + 取数结果 + ''' + p = 1 + while d > 1: + d -= 1 + p *= 10 + return num // p % 10 + + + if len(input_list) == 0: + return [] + sorted_list = input_list + length = len(sorted_list) + bucket = [0] * length + + for d in range(1, MaxBit(sorted_list) + 1): + count = [0] * 10 + + for i in range(0, length): + count[digit(sorted_list[i], d)] += 1 + + for i in range(1, 10): + count[i] += count[i - 1] + + for i in range(0, length)[::-1]: + k = digit(sorted_list[i], d) + bucket[count[k] - 1] = sorted_list[i] + count[k] -= 1 + for i in range(0, length): + sorted_list[i] = bucket[i] + + return sorted_list + +if __name__ == '__main__': + input_list = [50, 123, 543, 187, 49, 30, 0, 2, 11, 100] + print('排序前:', input_list) + sorted_list = RadixSort(input_list) + print('排序后:', sorted_list) From a00a79056ac7ccbfd9cfb0ef56117605d34b2fce Mon Sep 17 00:00:00 2001 From: duke <276877855@qq.com> Date: Fri, 13 Jul 2018 14:44:40 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=85=AB=E5=A4=A7=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 46a9d29..ac01f1b 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,26 @@ # Algorithm -* 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。
-* [我的个人网站](http://cuijiahua.com/ "悬停显示")
- -* 学习交流群【328127489】Coder
+* [博客](https://blog.csdn.net/u014793102 "悬停显示")
* 算法学习,持续更新中!
## 八大排序算法 -* Sort Algorithm.cpp - C++版
- * Sort Algorithm.py - Python版
-* [程序员内功:八大排序算法](http://cuijiahua.com/blog/2018/01/alogrithm_9.html "悬停显示")
- -### 原理说明: - -* [排序(1):冒泡排序](http://cuijiahua.com/blog/2017/12/algorithm_1.html "悬停显示")
+* 排序(1):冒泡排序 -* [排序(2):直接插入排序](http://cuijiahua.com/blog/2017/12/algorithm_2.html "悬停显示")
+* 排序(2):直接插入排序 -* [排序(3):希尔排序](http://cuijiahua.com/blog/2017/12/algorithm_3.html "悬停显示")
+* 排序(3):希尔排序 -* [排序(4):快速排序](http://cuijiahua.com/blog/2017/12/algorithm_4.html "悬停显示")
+* 排序(4):快速排序 -* [排序(5):简单选择排序](http://cuijiahua.com/blog/2017/12/algorithm_5.html "悬停显示")
+* 排序(5):简单选择排序 -* [排序(6):堆排序](http://cuijiahua.com/blog/2018/01/algorithm_6.html "悬停显示")
+* 排序(6):堆排序 -* [排序(7):归并排序](http://cuijiahua.com/blog/2018/01/algorithm_7.html "悬停显示")
+* 排序(7):归并排序 -* [排序(8):基数排序](http://cuijiahua.com/blog/2018/01/algorithm_8.html "悬停显示")
+* 排序(8):基数排序 From c5de56cf519f7e0bb4d168c72374e86f60662cfa Mon Sep 17 00:00:00 2001 From: duke <276877855@qq.com> Date: Thu, 16 Aug 2018 20:39:08 +0800 Subject: [PATCH 6/6] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ac01f1b..ac8c60a 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,5 @@ * 排序(7):归并排序 * 排序(8):基数排序 + +## 更新其他学习的新算法