definsertionSort(arr,n):'''
:param arr: 需要排序的序列
:param n: 降序(True)或者升序(False)
:return: 排序后的序列
'''for i inrange(len(arr)):
preIndex = i -1
current = arr[i]while preIndex >=0and arr[preIndex]<= current and n ==True:
arr[preIndex +1]= arr[preIndex]
preIndex -=1while preIndex >=0and arr[preIndex]> current and n ==False:
arr[preIndex +1]= arr[preIndex]
preIndex -=1
arr[preIndex +1]= current
return arr
2. JavaScript代码的实现
functioninsertionSort(arr){let preIndex,current;for(let i =1;i < arr.length;i++){
preIndex = i -1;
current = arr[i];while(preIndex >=0&& arr[preIndex]<= current && n ==true){
arr[preIndex +1]= arr[preIndex];
preIndex--;}while(preIndex >=0&& arr[preIndex]<= current && n ==false){
arr[preIndex +1]= arr[preIndex];
preIndex--;}
arr[preIndex +1]= current;}return arr;}
(四)希尔排序(进阶版的插入排序)
1. Python代码实现
defshellSort(arr,space,n):'''
:param arr: 需要排序的序列
:param space: 序列间隔
:param n: 降序(True)或者升序(False)
:return: 排序后的序列
'''
gap =1while gap <len(arr)/ space:
gap = gap * space +1while gap >0:for i inrange(gap,len(arr)):
j = i - gap
temp = arr[i]while j >=0and arr[j]<= temp and n ==True:
arr[j + gap]= arr[j]
j -= gap
while j >=0and arr[j]> temp and n ==False:
arr[j + gap]= arr[j]
j -= gap
arr[j + gap]= temp
gap = math.floor(gap / space)# 向下取整return arr
2. JavaScript代码的实现
functionshellSort(arr,space,n){let gap =1;while(gap < arr.length / space){
gap = gap * space +1;}while(gap >0){//此处,多了个gap,但与插入排序相比,有相似之处for(let i = gap;i < arr.length;i++){let temp = arr[i];for(let j = i - gap;j >=0&& arr[j]<= temp && n ==true;j -= gap){
arr[j + gap ]= arr[j];}for(let j = i - gap;j >=0&& arr[j]> temp && n ==false;j -= gap){
arr[j + gap ]= arr[j];}
arr[j + gap]= temp;}}return arr;}
(五)归并排序(分治法思想)
1. Python代码的实现
'''
归并排序实现的两种方法:
1. 自上而下的递归
2. 自下而上的迭代
此处,采用自上而下的递归方法,并将序列或子序列一分为二。
'''import math
defmergeSort(arr):iflen(arr)<2:return arr
middle = math.floor(len(arr)/2)
left,right = arr[0:middle],arr[middle:]return merge(mergeSort(left),mergeSort(right))defmerge(left,right):
result =[]while left and right:if left[0]< right[0]:
result.append(left.pop(0))else:
result.append(right.pop(0))while left:
result.append(left.pop(0))while right:
result.append(right.pop(0))return result
defquickSort(arr,left=None,right=None):
left =0ifnotisinstance(left,(int,float))else left
right =len(arr)-1ifnotisinstance(right,(int,float))else right
if left < right:
partitionIndex = partition(arr,left,right)
quickSort(arr,left,partitionIndex -1)
quickSort(arr,partitionIndex +1,right)return arr
defpartition(arr,left,right):
pivot = left
index = pivot +1
i = index
while i <= right:if arr[i]< arr[pivot]:
arr[i],arr[index]= arr[index],arr[i]
index +=1
i +=1
arr[pivot],arr[index -1]= arr[index -1],arr[pivot]return index -1
2. JavaScript代码的实现
functionquickSort(arr,left,right){let partitionIndex,
left =typeof left !='number'?0: left,
right =typeof right !='number' ? arr.length -1: right;if(left < right){
partitionIndex =partition(arr,left,right);quickSort(arr,left,partitonIndex -1);quickSort(arr,partitionIndex +1,right);}return arr;}functionpartition(arr,left,right){let pivot = left,index = pivot +1;for(let i = index;i <= right; i++){if(arr[i]< arr[pivot]){let temp = arr[i];
arr[i]= arr[index];
arr[index]= temp;
index++;}}let temp = arr[pivot];
arr[pivot]= arr[index -1];
arr[index -1]= temp;return index -1}
(七)堆排序
1. Python代码的实现
'''
堆排序的两种方法:
1. 大顶堆:每个节点的值大于或者等于其子节点的值,用于升序算法
2. 小顶堆:每个节点的值小于或者等于其子节点的值,用于降序算法
此处,采用大顶堆的方法进行排序。
'''import math
defbuildMaxHeap(arr):for i inrange(math.floor(len(arr)/2),-1,-1):
heapify(arr,i)defheapify(arr,i):
left =2* i +1
right =2* i +2
largest = i
if left < arrLen and arr[left]> arr[largest]:
largest = left
if right < arrLen and arr[right]> arr[largest]:
largest = right
if largest != i:
arr[i],arr[largest]= arr[largest],arr[i]
heapify(arr,largest)defheapSort(arr):global arrLen
arrLen =len(arr)
buildMaxHeap(arr)for i inrange(len(arr)-1,0,-1):
arr[0],arr[i]= arr[i],arr[0]
arrLen -=1
heapify(arr,0)return arr
2.JavaScript代码的实现
let len;functionbuildMaxHeap(arr){
len = arr.length;for(let i = Math.floor(len /2);i >=0;i--){heapify(arr,i);}}functionheapify(arr,i){let left =2* i +1,
right =2* i +2,
largest = i;if(left < len && arr[left]> arr[largest]{
largest = left;}if(right < len && arr[right]> arr[largest]{
largest = right;}if(largest != i){let temp = arr[i];
arr[i]= arr[largest];
arr[largest]= temp;heapify(arr,largest);}}functionheapSort(arr){buildMaxHeap(arr);for(let i =arr.length -1; i >0; i--){let temp = arr[0];
arr[0]= arr[i];
arr[i]= twmp;
len--;heapify(arr,0);}return arr;}