一、知识点
十大排序,详情见第二个例题的代码和注释
二、例题
1.合并两个有序数组
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
index1,index2,index=m-1,n-1,m+n-1
while index1>=0 and index2>=0:
if nums1[index1]<nums2[index2]:
nums1[index]=nums2[index2]
index2-=1
else:
nums1[index]=nums1[index1]
index1-=1
index-=1
nums1[:index2+1]=nums2[:index2+1]
2.排序数组
class Solution:
def bubbleSort(self,arr):
for i in range(len(arr)):
for j in range(len(arr)-i-1):
if arr[j]>arr[j+1]:
arr[j],arr[j+1]=arr[j+1],arr[j]
return arr
def selectionSort(self,arr):
for i in range(len(arr)-1):
min_i=i
for j in range(i+1,len(arr)):
if arr[j]<arr[min_i]:
min_i=j
if min_i!=i:
arr[i],arr[min_i]=arr[min_i],arr[i]
return arr
def insertionSort(self,arr):
for i in range(1,len(arr)):
temp=arr[i]
j=i
while j>0 and arr[j-1]>temp:
arr[j]=arr[j-1]
j-=1
arr[j]=temp
return arr
def shellSort(self,arr):
gap=len(arr)//2
while gap>0:
for i in range(gap,len(arr)):
temp=arr[i]
j=i
while j>gap-1 and arr[j-gap]>temp:
arr[j]=arr[j-gap]
j-=gap
arr[j]=temp
gap=gap//2
return arr
def merge(self,left_arr,right_arr):
arr=[]
while left_arr and right_arr:
if left_arr[0] <= right_arr[0]:
arr.append(left_arr.pop(0))
else:
arr.append(right_arr.pop(0))
while left_arr:
arr.append(left_arr.pop(0))
while right_arr:
arr.append(right_arr.pop(0))
return arr
def mergeSort(self,arr):
if len(arr)<2:
return arr
mid=len(arr)//2
left_arr,right_arr=arr[:mid],arr[mid:]
return self.merge(self.mergeSort(left_arr),self.mergeSort(right_arr))
def partition(self,arr,low,high):
i=low-1
pivot=arr[high]
for j in range(low,high):
if arr[j]<=pivot:
i+=1
arr[i],arr[j]=arr[j],arr[i]
arr[i+1],arr[high]=arr[high],arr[i+1]
return i+1
def randomPartition(self,arr,low,high):
i=random.randint(low,high)
arr[i],arr[high]=arr[high],arr[i]
return self.partition(arr,low,high)
def quickSort(self,arr,low,high):
if low<high:
pi=self.randomPartition(arr,low,high)
self.quickSort(arr,low,pi-1)
self.quickSort(arr,pi+1,high)
return arr
def heapify(self,arr,index,end):
left=index*2+1
right=left+1
while left<=end:
max_index=index
if arr[left]>arr[max_index]:
max_index=left
if right<=end and arr[right]>arr[max_index]:
max_index=right
if index==max_index:
break
arr[index],arr[max_index]=arr[max_index],arr[index]
index=max_index
left=index*2+1
right=left+1
def buildMaxHeap(self,arr):
size=len(arr)
for i in range((size-2)//2,-1,-1):
self.heapify(arr,i,size-1)
return arr
def maxHeapSort(self,arr):
self.buildMaxHeap(arr)
size=len(arr)
for i in range(size):
arr[0],arr[size-i-1]=arr[size-i-1],arr[0]
self.heapify(arr,0,(size-1)-(i+1))
return arr
def countingSort(self,arr):
arr_min,arr_max=min(arr),max(arr)
size=arr_max-arr_min+1
counts=[0 for _ in range(size)]
for num in arr:
counts[num-arr_min]+=1
for j in range(1,size):
counts[j]+=counts[j-1]
res=[0 for _ in range(len(arr))]
for i in range(len(arr)):
res[counts[arr[i]-arr_min]-1]=arr[i]
counts[arr[i]-arr_min]-=1
return res
def bucketSort(self,arr,bucket_size=5):
arr_min,arr_max=min(arr),max(arr)
bucket_count=(arr_max-arr_min)//bucket_size+1
buckets=[[] for _ in range(bucket_count)]
for num in arr:
buckets[(num-arr_min)//bucket_size].append(num)
res=[]
for bucket in buckets:
self.insertionSort(bucket)
res.extend(bucket)
return res
def radixSort(self,arr):
size=len(str(max(arr)))
for i in range(size):
buckets=[[] for _ in range(10)]
for num in arr:
buckets[num//(10**i)%10].append(num)
arr.clear()
for bucket in buckets:
for num in bucket:
arr.append(num)
return arr
def sortArray(self, nums: List[int]) -> List[int]:
return self.bucketSort(nums)
3.有序数组的平方
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
i,j,k=0,len(nums)-1,len(nums)-1
ans=[-1]*len(nums)
while i<=j:
if nums[i]**2>nums[j]**2:
ans[k]=nums[i]**2
i+=1
else:
ans[k]=nums[j]**2
j-=1
k-=1
return ans
4.数组的相对排序
class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
min_arr2,max_arr2=min(arr2),max(arr2)
counts=[0]*(max_arr2-min_arr2+1)
rest=[]
for num in arr1:
if num not in arr2:
rest.append(num)
else:
counts[num-min_arr2]+=1
ans=[]
for num in arr2:
for i in range(counts[num-min_arr2]):
ans.append(num)
return ans+sorted(rest)
5.对链表进行插入排序
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
dummy=ListNode(next=head)
while head and head.next:
temp=head.next
if temp.val>=head.val:
head=head.next
continue
else:
pre=dummy
while pre.next.val<=temp.val:
pre=pre.next
head.next=head.next.next
temp.next=pre.next
pre.next=temp
return dummy.next
|