[TOC]
<br>
### 排序實例
>[info]
>通過`多種排序算法`對一組無序數據進行排序算法設計,要求如下:
>輸入:[1, 3, 5, 23, 75, 34, 456, 86, 22, 74, 37, 5, 34]
>輸出:[1, 3, 5, 5, 22, 23, 34, 34, 37, 74, 75, 86, 456]
#### 冒泡排序
核心算法:循環比較相鄰的兩個元素,如果前面一個元素比后面一個元素大,則交換位置。
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def bubble_sort(data_source):
length = len(data_source)
for i in range(1, length):
for j in range(length - i):
if data_source[j] > data_source[j + 1]:
data_source[j], data_source[j + 1] = data_source[j + 1], data_source[j]
return data_source
if __name__ == '__main__':
test_array = [1, 3, 5, 23, 75, 34, 456, 86, 22, 74, 37, 5, 34]
print(bubble_sort(test_array))
```
#### 插入排序
核心算法:從頭到尾循環,對于未排序數據,在已排序序列中從后向前掃描,找到相應位置并插入,因而在從后向前掃描過程中,需要反復把已排序元素逐步向后挪位,為最新元素提供插入空間。
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def insert_sort(data_source):
count = len(data_source)
for i in range(1, count):
key = data_source[i]
j = i - 1
while j >= 0:
if data_source[j] > key:
data_source[j], data_source[j + 1] = key, data_source[j]
j -= 1
return data_source
if __name__ == '__main__':
test_array = [1, 3, 5, 23, 75, 34, 456, 86, 22, 74, 37, 5, 34]
print(insert_sort(test_array))
```
#### 快速排序
核心算法:每次循環,取一個基數,將序列分成三部分:比基數小的數序列,基數,比基數大的序列。不斷重復對每個序列進行相同的處理,直到每個序列為空,則完成排序
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def quick_sort(data_source):
length = len(data_source)
if length == 0:
return []
else:
left = []
right = []
for i in range(1, length):
if data_source[0] > data_source[i]:
left.append(data_source[i])
else:
right.append(data_source[i])
return quick_sort(left) + [data_source[0]] + quick_sort(right)
if __name__ == '__main__':
test_array = [1, 3, 5, 23, 75, 34, 456, 86, 22, 74, 37, 5, 34]
print(quick_sort(test_array))
```
### 二分法查找實例
二分法,主要應用于有序序列中,原理是每次查找都將原序列折半,逐漸縮小查找范圍的一種算法。
>[info]要求在一個有序序列中,例如[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99],查找一個數字,如果找到則打印該數字,如果找不到,則輸出“not found!”
#### 遞歸方式
遞歸,是在函數中自身調用自身的一種情況,直到有一個明確的退出條件成立后結束相互調用。遞歸是一種很容易理解某類問題的方式,但是不是特別高效,因為每次調用自身時,都會在內存中創建一個新的內存空間,當不斷循環調用非常多次時,是非常耗內存的。
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def recursion_search(data_source, find_n):
mid = int(len(data_source) / 2)
if len(data_source) >= 1:
if find_n > data_source[mid]:
recursion_search(data_source[mid + 1:], find_n)
elif find_n < data_source[mid]:
recursion_search(data_source[:mid], find_n)
else:
print(data_source[mid])
else:
print("not found !")
if __name__ == '__main__':
test_array = range(0, 100, 3)
recursion_search(test_array, 42)
```
#### 循環方式
```python
#!/usr/bin/python
# coding=utf-8
def loop_search(data_source, find_n):
while len(data_source):
mid = int(len(data_source) / 2)
if find_n < data_source[mid]:
data_source = data_source[:mid]
elif find_n > data_source[mid]:
data_source = data_source[mid + 1:]
else:
print(data_source[mid])
break
else:
print("not found !")
if __name__ == '__main__':
test_array = range(0, 100, 3)
loop_search(test_array, 45)
```
<hr style="margin-top:100px">
:-: 
***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***
- 前言
- chapter01_開發環境
- chapter02_字符串的使用
- chapter03_列表的使用
- chapter04_字典的使用
- chapter05_數字的使用
- chapter06_元組的使用
- chapter07_集合的使用
- chapter08_輸入輸出
- chapter09_控制流程
- chapter10_實例練習_登錄1
- chapter11_python函數入門
- chapter12_python中的類
- chapter13_輕松玩轉python中的模塊管理
- chapter14_掌握學習新模塊的技巧
- chapter15_通過os模塊與操作系統交互
- chapter16_子進程相關模塊(subprocess)
- chapter17_時間相關模塊(time & datetime)
- chapter18_序列化模塊(json)
- chapter19_加密模塊(hashlib)
- chapter20_文件的讀與寫
- chapter21_階段考核2_登錄
- chapter22_小小算法挑戰(排序&二分法)
- chapter23_用多線程來搞事!
- chapter24_HTTP接口請求(requests)
- chapter25_接口測試框架(pytest)
- chapter26_階段考核3_HTTP接口測試
- chapter27_HTML解析(pyquery)
- chapter28_階段考核4_爬蟲下載網易汽車
- chapter29_python中的那些編碼坑
- chapter30_MySQL數據庫操作
- chapter31 高級特性_迭代器與生成器
- chapter32 高級特性_裝飾器
- chapter33 高級特性_列表處理