Skip to content

Commit 0028c74

Browse files
author
tianqing.liang
committed
归并排序,快速排序
1 parent 1951c8b commit 0028c74

File tree

3 files changed

+213
-5
lines changed

3 files changed

+213
-5
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# AlgorithmAndDataArchitecture
22

3-
<h3>使用Flutter的Dart语言重写数据结构与算法</h3>
3+
<h3>使用Python、Flutter的Dart语言重写数据结构与算法</h3>
4+
45

56
1. **[Dart,Python]线性搜索**
67
2. **[Dart,Python]选择排序**
@@ -9,7 +10,7 @@
910
5. 链表,链表实现栈,链表实现队列
1011
6. 递归
1112
7. 归并排序
12-
8. 快速排序
13+
8. **[Dart,Python]快速排序**
1314
9. 二分搜索
1415
10. 二分搜索树
1516
11. 集合 和 映射

python/linkedlist.py

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
class __Node(object):
2+
3+
def __init__(self,value=None,next=None):
4+
self.value = value;
5+
self.next = next;
6+
7+
def __str__(self):
8+
return self.value;
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
# class LinkedList<T> {
31+
# _Node? _dummyHead;
32+
# late int _size;
33+
34+
# LinkedList() {
35+
# _dummyHead = new _Node.withEmpty();
36+
# _size = 0;
37+
# }
38+
39+
# int getSize() {
40+
# return _size;
41+
# }
42+
43+
# bool isEmpty() {
44+
# return _size == 0;
45+
# }
46+
47+
# remove(int index){
48+
# if (index < 0 || index > _size) {
49+
# throw Exception("Remove Failed,Illegal index");
50+
# }
51+
# _Node? prev = _dummyHead;
52+
# for(var i =0;i<index ;i++){
53+
# prev = prev?.next;
54+
# }
55+
# _Node? retNode = prev!.next;
56+
# prev.next = retNode!.next;
57+
# retNode.next = null;
58+
# _size --;
59+
# return retNode.t;
60+
# }
61+
62+
# //从链表中删除第一个元素,返回删除元素
63+
# T? removeFirst(){
64+
# remove(0);
65+
# }
66+
# //从链表中删除第一个元素,返回删除元素
67+
# T? removeLast(){
68+
# remove(_size -1);
69+
# }
70+
71+
72+
# //在链表的index位置添加新的元素e
73+
# add(int index, T t) {
74+
# if (index < 0 || index > _size) {
75+
# throw Exception("Add Failed,Illegal index");
76+
# }
77+
# // if(index == 0){
78+
# // addFirst(t);
79+
# // }else{
80+
# _Node? prev = _dummyHead;
81+
# for (var i = 0; i < index; i++) {
82+
# prev = prev?.next;
83+
# }
84+
# _Node _node = new _Node.wihtHead(t);
85+
# _node.next = prev?.next;
86+
# prev?.next = _node;
87+
# _size++;
88+
# // }
89+
# }
90+
91+
# addLast(T t) {
92+
# add(_size, t);
93+
# }
94+
95+
# addFirst(T t) {
96+
# // _Node _node =new _Node.head(t);
97+
# // _node.next = _head;
98+
# // _head = _node;
99+
# add(0, t);
100+
# }
101+
102+
# //获取链表第index位置的元素
103+
# T get(int index){
104+
# if (index < 0 || index > _size) {
105+
# throw Exception("Get Failed,Illegal index");
106+
# }
107+
# _Node? cur = _dummyHead!.next;
108+
# for(var i=0;i<index;i++){
109+
# cur = cur!.next;
110+
# }
111+
# return cur!.t;
112+
# }
113+
114+
# T getFirst(){
115+
# return get(0);
116+
# }
117+
# T getLast(){
118+
# return get(_size-1);
119+
# }
120+
# set(int index,T t){
121+
# if (index < 0 || index > _size) {
122+
# throw Exception("Set Failed,Illegal index");
123+
# }
124+
# _Node? cur = _dummyHead!.next;
125+
# for(var i =0;i<index;i++){
126+
# cur = cur!.next;
127+
# }
128+
# cur!.t= t;
129+
# }
130+
131+
# bool contains(T t){
132+
# _Node? cur = _dummyHead!.next;
133+
# for(var i =0;i<_size-1;i++){
134+
# if(cur!.t.compareTo(t)){
135+
# return true;
136+
# }
137+
# cur = cur.next;
138+
# }
139+
# return false;
140+
# }
141+
142+
# @override
143+
# String toString() {
144+
# StringBuffer res =new StringBuffer();
145+
# for(_Node? cur = _dummyHead!.next ; cur != null ; cur = cur.next){
146+
# res.write(cur);
147+
# res.write("->");
148+
# }
149+
# res.write("NULL");
150+
# return res.toString();
151+
# }
152+
# }
153+
154+

python/multiSort.py

+56-3
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,75 @@ def shellSort(sortList):
6666
j = j- dist
6767
dist = dist//2
6868
return sortList
69+
70+
#快速排序
71+
def quickSort(sortList,left,right):
72+
if left<right:
73+
q = partition(sortList,left,right)
74+
quickSort(sortList,left,q-1)
75+
quickSort(sortList, q+1, right)
76+
77+
def partition(sortList,left,right):
78+
i = left-1
79+
pivot = sortList[right]
80+
for j in range(left,right):
81+
if sortList[j] <= pivot:
82+
i +=1
83+
sortList[i],sortList[j] = sortList[j],sortList[i]
84+
sortList[i+1],sortList[right] = sortList[right],sortList[i+1]
85+
return i +1
86+
87+
# 归并排序
88+
def merge(arr1, arr2):
89+
result = []
90+
while arr1 and arr2:
91+
if arr1[0] < arr2[0]:
92+
result.append(arr1.pop(0))
93+
else:
94+
result.append(arr2.pop(0))
95+
if arr1:
96+
result += arr1
97+
if arr2:
98+
result += arr2
99+
return result
100+
101+
def merge_sort(arr):
102+
if len(arr) <= 1:
103+
return arr
104+
mid = len(arr) // 2
105+
return merge(merge_sort(arr[:mid]), merge_sort(arr[mid:]))
106+
69107
# 计算结果,计算时间
70108
def caculateResultAndTime(name,fuction,randomseed):
71-
print("--------------------",name)
109+
print("--------------------",name,'\n')
72110
random.seed(randomseed)
73-
sortList = [random.randint(0,100) for _ in range(10)]
111+
sortList = [random.randint(0,100) for _ in range(30)]
74112
print("原始数据:",sortList)
75113
startTime = time.perf_counter()
76114
fuction(sortList)
77115
endTime = time.perf_counter()
78-
print("排序结果:",sortList,' 花费时间:',endTime-startTime)
116+
print("排序结果:",sortList,'\n花费时间:',endTime-startTime)
117+
print("-------------------- 结束\n")
79118

80119
if __name__ == '__main__':
81120
caculateResultAndTime("希尔排序",shellSort,88)
82121
caculateResultAndTime("插入排序",insertionSort,89)
83122
caculateResultAndTime("冒泡排序",bubble_sort,90)
84123
caculateResultAndTime("选择排序",selectionSort,91)
124+
caculateResultAndTime("归并排序",selectionSort,93)
125+
126+
print("--------------------快速排序")
127+
random.seed(92)
128+
sortList = [random.randint(0,100) for _ in range(15)]
129+
print("原始数据:",sortList)
130+
startTime = time.perf_counter()
131+
quickSort(sortList, 0, len(sortList)-1)
132+
endTime = time.perf_counter()
133+
print("排序结果:",sortList,' 花费时间:',endTime-startTime)
134+
print("-------------------- 结束\n")
135+
136+
137+
85138

86139

87140

0 commit comments

Comments
 (0)