Skip to content

Commit 00148c0

Browse files
author
tianqing.liang
committed
新增python算法和数据结构
1 parent dad74df commit 00148c0

File tree

193 files changed

+52
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+52
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

python/sort.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
线性查找
3+
冒泡排序
4+
选择排序
5+
插入排序
6+
希尔排序
7+
'''
8+
import random
9+
# 线性查找算法
10+
def liner_search(list,value):
11+
for item in range(len(list)):
12+
if(list[item] == value):
13+
return item;
14+
return -1;
15+
print("---------------------线性查找");
16+
testList = [1,2,3,4,5,6,7,8,9]
17+
print(liner_search(testList, 8))
18+
19+
# 选择排序
20+
def selectionSort(sortList):
21+
print(sortList);
22+
for i in range(len(sortList)):
23+
minIndex = i ;
24+
for j in range(i,len(sortList)):
25+
if sortList[minIndex]>sortList[j]:
26+
minIndex=j
27+
sortList[i],sortList[minIndex] = sortList[minIndex],sortList[i]
28+
return sortList
29+
print("---------------------选择排序");
30+
random.seed(54)
31+
sortList = [random.randint(0,100) for _ in range(10)]
32+
print("原始数据:", sortList)
33+
selectionSort(sortList)
34+
print("选择排序结果:", sortList)
35+
36+
37+
# 冒泡排序
38+
def bubble_sort(sortList):
39+
for i in range(len(sortList)):
40+
isSwap = False; #设置标志位,若已排序好,则不排序
41+
for j in range(len(sortList)-i-1):
42+
if sortList[j] > sortList[j + 1]:
43+
sortList[j], sortList[j + 1] = sortList[j + 1], sortList[j]
44+
isSwap = True
45+
if not isSwap:
46+
break
47+
print("---------------------冒泡排序");
48+
random.seed(54)
49+
sortList = [random.randint(0,100) for _ in range(10)]
50+
print("原始数据:", sortList)
51+
bubble_sort(sortList)
52+
print("冒泡排序结果:", sortList)

0 commit comments

Comments
 (0)