常見排序演算法-Python實現
常見排序演算法-Python實現
python
排序
演算法
1.二分法
-
#coding=utf-8
-
def binary_search(input_array, value):
- “””Your code goes here.”””
- length = len(input_array)
- left = 0
- right = length-1
- if length == 1:
- return 0 if value == input_value[0] else –1
- else:
- mid = (left+right)/2
- while(right-left>1):
- if input_array[mid] == value:
- return mid
- elif input_array[mid] > value:
- right = mid
- mid = (left+right)/2
- else:
- left = mid
- mid = (left+right)/2
- if input_array[left] == value:
- return left
- elif input_array[right] == value:
- return right
- else:
- return –1
-
- test_list = [1,3,9,11,15,19,29]
- test_val1 = 25
- test_val2 = 15
-
print (binary_search(test_list, test_val1))
-
print (binary_search(test_list, test_val2))
2.冒泡法
-
#coding=utf-8
-
-
# way=1遞增排序 way=0遞減排序
-
def bubble_sort(array,way=1):
- length = len(array)
-
- if not length:
- print(“Error!The length of array must be greater than 0.
“)
- return `Wrong array`
-
- if way == 1:
- while length > 0:
- for i in range(length-1):
- if array[i] > array[i+1]:
- array[i],array[i+1] = array[i+1],array[i]
- length -= 1
- return array
-
- elif way == 0:
- while length > 0:
- for i in range(length-1):
- if array[i] < array[i+1]:
- array[i],array[i+1] = array[i+1],array[i]
- length -= 1
- return array
-
-
# 加入排序判斷標誌,可提高執行效率
-
# way=1遞增排序 way=0遞減排序
-
def better_bubble_sort(array,way=1):
- is_sorted = True # 判斷記錄上次遍歷是否進行過交換,若沒有則表示不用再遍歷了
- length = len(array)
-
- if not length:
- print(“Error!The length of array must be greater than 0.
“)
- return `Wrong array`
-
- if way == 1:
- while length > 0 and is_sorted:
- for i in range(length-1):
- is_sorted = False
- if array[i] > array[i+1]:
- array[i],array[i+1] = array[i+1],array[i]
- is_sorted = True
- length -= 1
- return array
-
- elif way == 0:
- while length > 0 and is_sorted:
- for i in range(length-1):
- is_sorted = False
- if array[i] < array[i+1]:
- array[i],array[i+1] = array[i+1],array[i]
- is_sorted = True
- length -= 1
- return array
-
-
- test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]
- print(better_bubble_sort(test,1))
3.插入排序
-
#coding=utf-8
-
-
def insert_sort(array):
- length = len(array)
- flag = array[0]
- for x in range(1,length):
- # 之前的
- if array[x] < array[x-1]:
- flag = array[x]
- y = x
- while y != 0 and array[y-1] > flag :
- array[y] = array[y-1]
- y -= 1
- array[y] = flag
- return array
-
- test = [21, 4, 1, 3, 9, 20, 25, 20, 3]
- print(insert_sort(test))
4.歸併排序
-
#coding=utf-8
-
-
def merge_sort(array):
- if len(array) <= 1:
- return array
-
- split_index = len(array)/2
- left = merge_sort(array[:split_index])
- right = merge_sort(array[split_index:])
- return merge(left,right)
-
-
def merge(left,right):
- i = 0
- j = 0
- result = []
- while i < len(left) and j < len(right):
- if left[i] <= right[j]:
- result.append(left[i])
- i += 1
- else:
- result.append(right[j])
- j += 1
-
- result += (left[i:])
- result += (right[j:])
- return result
-
- a = [1,2]
- test = [21, 4, 1, 3, 9, 20, 25]
- print(merge_sort(test))
5.選擇排序
-
#coding=utf-8
-
-
def select_sort(array):
- length = len(array)
- # mini = array[0]
- for i in range(length):
- mini = array[i]
- for j in range(i,length):
- if array[j] < mini:
- mini = array[j]
- array[i],array[j] = array[j],array[i]
- return array
-
- test = [21, 4, 1, 3, 9, 20, 25, 20, 3]
- print(select_sort(test))
6.快速排序
-
#coding=utf-8
-
-
# 遞迴
-
def quick_sort(lists, left, right):
- # 快速排序
- if left >= right:
- return lists
- key = lists[left]
- low = left
- high = right
- while left < right:
- while left < right and lists[right] >= key:
- right -= 1
- lists[left] = lists[right]
- while left < right and lists[left] <= key:
- left += 1
- lists[right] = lists[left]
- lists[right] = key
- quick_sort(lists, low, left – 1)
- quick_sort(lists, left + 1, high)
- return lists
-
-
- test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]
-
print (quick_sort(test,0,len(test)-1))
written by MARSGGBO
2017-2-14
相關文章
- python實現常見的五種排序演算法Python排序演算法
- 常見排序原理及 python 實現排序Python
- 常見演算法 PHP 實現 -- 堆排序演算法PHP排序
- Haskell常見排序演算法的實現Haskell排序演算法
- python3實現幾種常見的排序演算法Python排序演算法
- 常見排序演算法原理及JS程式碼實現排序演算法JS
- 常見排序演算法排序演算法
- Python實現常見機器學習演算法(上)Python機器學習演算法
- 10種python常見的排序演算法!Python排序演算法
- 用 Java 實現常見的 8 種內部排序演算法Java排序演算法
- 常見的排序演算法 (下)排序演算法
- 常見排序演算法總結排序演算法
- 常見排序演算法小結排序演算法
- Java常見排序演算法之插入排序Java排序演算法
- 演算法之常見排序演算法-氣泡排序、歸併排序、快速排序演算法排序
- python排序演算法的實現-快速排序Python排序演算法
- 通過leetcode學習常見排序演算法及其Go實現LeetCode排序演算法Go
- 常見的排序演算法分析(一)排序演算法
- PHP常見排序演算法學習PHP排序演算法
- python實現希爾排序演算法Python排序演算法
- JavaScript實現常見查詢演算法JavaScript演算法
- Javascript常見排序演算法的筆記JavaScript排序演算法筆記
- 常見排序演算法及複雜度排序演算法複雜度
- 幾種常見排序演算法總結排序演算法
- 基本排序演算法的Python實現排序演算法Python
- python排序演算法的實現-冒泡Python排序演算法
- python排序演算法的實現-插入Python排序演算法
- python演算法 - python實現氣泡排序Python演算法排序
- 35.幾種常見的排序演算法排序演算法
- 幾種常見的排序演算法總結排序演算法
- 常見排序演算法及其實現(Binary,Insert、Select、Quick、Bubble.etc.Sort)排序演算法UI
- python實現氣泡排序、插入排序以及快速排序演算法Python排序演算法
- python實現常用五種排序演算法Python排序演算法
- 用 python 實現各種排序演算法Python排序演算法
- python排序演算法的實現-選擇Python排序演算法
- 棧的模擬實現及常見演算法演算法
- 常見的排序演算法:冒泡、快排、歸併排序演算法
- 排序演算法原理總結和Python實現排序演算法Python