#!usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2019/3/28 10:26
# @Author : Yosef-夜雨声烦
# @Email : wurz529@foxmail.com
# @File : sort.py
# @Software : PyCharm
def bubble_sort(arr):
'''
冒泡排序
:param arr:
:return:
'''
count = len(arr)
for i in range(count):
for j in range(count - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(arr)
def selection_sort(arr):
"""
选择排序
:param arr:
:return:
"""
count = len(arr)
for i in range(0, count - 1):
index = i
for j in range(i + 1, count):
if arr[index] > arr[j]:
index = j
arr[i], arr[index] = arr[index], arr[i]
print(arr)
def insert_sort(arr):
'''
插入排序
:param arr:
:return:
'''
for i in range(1, len(arr)):
j = i - 1
key = arr[i]
while j >= 0:
if arr[j] > key:
arr[j + 1] = arr[j]
arr[j] = key
j -= 1
print(arr)
def quick_sort(data, start, end):
'''
快速排序
:param data:
:param start:
:param end:
:return:
'''
i = start
j = end
# i与j重合时,一次排序结束
if i >= j:
return
# 设置最左边的数为基准值
flag = data[start]
while i < j:
while i<j and data[j] >= flag:
j -= 1
# 找到右边第一个小于基准的数,赋值给左边i。此时左边i被记录在flag中
data[i] = data[j]
while i<j and data[i] <= flag:
i += 1
# 找到左边第一个大于基准的数,赋值给右边的j。右边的j的值和上面左边的i的值相同
data[j] = data[i]
# 由于循环以i结尾,循环完毕后把flag值放到i所在位置。
data[i] = flag
# 除去i之外两段递归
quick_sort(data, start, i-1)
quick_sort(data, i+1, end)
arr = [3, 6, 4, 2, 11, 10, 5]
quick_sort(arr,0,len(arr)-1)
print(arr)