排序分为内部排序和外部排序。
内部排序就是在内存中排序,如果数据很大,不能全部读入内存,就需要访问外村,称为外部排序。
def sort(arry):
n=len(arry)
def quicksort(arry):
imp=arry[0],
for i in range(1,len(arry)):
import random
def insert_sort(nums):
"""
# 插入排序
:param nums:
:return:
"""
count = len(nums)
for i in range(1, count):
key = nums[i]
j = i - 1
while j >= 0:
if nums[j] > key:
nums[j + 1] = nums[j]
nums[j] = key
j -= 1
return nums
if __name__ == '__main__':
nums = [random.randint(1, 1000) for i in range(1000)]
sort_nums = insert_sort(nums)
print(sort_nums)
def shellSort(arr):
"""
5 4 3 2 1
"""
step = int(len(arr) / 2) # step
while step > 0:
print("step=", step)
arr_len = len(arr)
# index = 0 1 2 3 4
for index in range(arr_len):
# index=0 index+step=3
# index=1 indx+step=4
if index + step < arr_len:
current_val = arr[index]
if current_val > arr[index + step]:
arr[index], arr[index + step] = arr[index + step], arr[index]
step = int(step / 2)
else:
# 直接插入排序
return insert_sort(arr)
def findSmallest(arr):
smallest = arr[0]
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selectSort(arr):
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
newArr.append(arr.pop(smallest))
return newArr