汉语版:我们将使用Python来实现内部排序的几个算法,很容易就实现了各种内部排序。
E-Version: here are some inner sort method including bubble sort, select sort, insert sort, quick sort, merge sort ,heap sort and other sort method. we can implement them easily by using python. I will just post some codes and you can copy or paste them in your code lines. but you should prepare that not all the codes are right , some codes contains errors if you check it carefully, you will find them.
一:冒泡排序
# 冒泡排序
def bubble_sort(sort_number=[], sort_type='false'):
result = []
length = len(sort_number)
while length > 0:
for j in range(length-1):
if sort_number[j] > sort_number[j+1]:
sort_number[j], sort_number[j+1] = sort_number[j+1], sort_number[j]
length -= 1
print(sort_number)
result.append(sort_number.copy())
print(result)
return result
# 选择排序
def select_sort(sort_number=[], sort_type='false'):
result = []
for i in range(len(sort_number)):
min_index = i
for j in range(i+1, len(sort_number)):
if sort_number[min_index] > sort_number[j]:
min_index = j
if min_index != i:
sort_number[min_index], sort_number[i] = sort_number[i], sort_number[min_index]
print(sort_number)
result.append(sort_number.copy())
print(result)
return result
# 插入排序
def insert_sort(sort_number=[], sort_type='false'):
result = []
for i in range(1, len(sort_number)):
for j in range(i, 0, -1):
if sort_number[j] < sort_number[j-1]:
sort_number[j], sort_number[j-1] = sort_number[j-1], sort_number[j]
print(sort_number)
result.append(sort_number.copy())
print(result)
return result
# 希尔排序
def shell_sort(sort_number=[], sort_type='false'):
result = []
length = len(sort_number)
shell = 1
while shell < length / 3:
shell = 3 * shell + 1
while shell >= 1:
for i in range(shell, length):
j = i
while j >= shell and sort_number[j] < sort_number[j - shell]:
sort_number[j], sort_number[j - shell] = sort_number[j-shell], sort_number[j]
j -= shell
shell = shell // 3
print(sort_number)
result.append(sort_number.copy())
print(result)
return result
# 归并排序
def merge_sort(sort_number=[], sort_type='false'):
if len(sort_number) <= 1:
return sort_number, []
length = int(len(sort_number)/2)
left, a = merge_sort(sort_number[:length])
right, b = merge_sort(sort_number[length:])
return merge(left, right), merge_result
# 归并工具_1
def merge(left, right):
i, j = 0, 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:]
merge_result.append(result.copy())
return result
# 快速排序
def quick_sort(sort_number=[], sort_type='false'):
quick(sort_number, 0, len(sort_number)-1)
return quick_result
# 快速排序工具_1
def quick(sort_number, left, right):
if left >= right:
return sort_number
else:
p = part(sort_number, left, right)
quick_result.append(sort_number.copy())
quick(sort_number, left, p - 1)
quick(sort_number, p + 1, right)
return sort_number
# 快速排序工具_2
def part(sort_number, left, right):
key = sort_number[left]
low = left
high = right
while low < high:
while (low < high) and (sort_number[high] >= key):
high -= 1
sort_number[low] = sort_number[high]
while (low < high) and (sort_number[low] <= key):
low += 1
sort_number[high] = sort_number[low]
sort_number[low] = key
return low
# 基数排序
def index_sort(sort_number=[], sort_type='false'):
result = []
k = int(math.ceil(math.log(max(sort_number), 10)))
bucket = [[] for i in range(10)]
for i in range(1, k + 1):
for j in sort_number:
bucket[int(j / (10 ** (i - 1)) % (10 ** i))].append(j)
del sort_number[:]
for z in bucket:
sort_number += z
del z[:]
result.append(sort_number.copy())
return result
# 堆排序
def heap_sort(sort_number=[], sort_type='false'):
size = len(sort_number)
sort_number = build_heap(sort_number, size)
print(sort_number)
print("############################")
for i in range(size-1, -1, -1):
sort_number[0], sort_number[i] = sort_number[i], sort_number[0]
shift_down(sort_number, 0, i)
print(sort_number)
# 堆排序工具_1
def shift_down(sort_number, position, size):
left_child = 2*position + 1
right_child = 2*position + 2
max_p = position
if left_child < size and sort_number[left_child] > sort_number[max_p]:
max_p = left_child
if right_child < size and sort_number[left_child] > sort_number[max_p]:
max_p = right_child
if max_p != position:
sort_number[max_p], sort_number[position] = sort_number[position], sort_number[max_p]
shift_down(sort_number, max_p, size)
return sort_number
# 堆排序工具_2
def build_heap(sort_number, size):
for i in range(size//2+1):
sort_number = shift_down(sort_number, i, size)
return sort_number
# 字符串转数值列表
def string_to_number_list(sort_number):
# 校验是否存在非数字非分割字符
for i in range(len(sort_number)):
if sort_number[i] < '0' or sort_number[i] > '9':
if sort_number[i] == ';':
continue
else:
return 'un_valid'
string_list = sort_number.split(';')
number_list = []
for i in range(len(string_list)):
temp = int(string_list[i])
number_list.append(temp)
return number_list
import os
import uuid
from django.http import HttpResponse, StreamingHttpResponse
from django.shortcuts import render, render_to_response
from inner_tools import sort_tools as tools
# Create your views here.
def inner_sort(request):
return render(request, 'sort.html')
def sort_result(request):
ctx = {}
sort_number = request.POST['sort_number']
sort_method = request.POST['sort_method']
numbers = tools.string_to_number_list(sort_number)
# 存放结果
result = ''
if sort_method == 'bubble_sort':
result = tools.bubble_sort(numbers)
elif sort_method == 'select_sort':
result = tools.select_sort(numbers)
elif sort_method == 'insert_sort':
result = tools.insert_sort(numbers)
elif sort_method == 'shell_sort':
result = tools.shell_sort(numbers)
elif sort_method == 'merge_sort':
a, result = tools.merge_sort(numbers)
elif sort_method == 'quick_sort':
result = tools.quick_sort(numbers)
elif sort_method == 'index_sort':
result = tools.index_sort(numbers)
elif sort_method == 'heap_sort':
result = tools.heap_sort(numbers)
ctx['result'] = result
return render(request, 'sort.html', ctx)
内部排序实例算法
请按照指导进行操作!
{% if result %}
{% for foo in result %}
第{{ forloop.counter }}次:{{ foo }}
{% endfor %}
{% endif %}