class tenSortALG(object):
"""docstring for tenSortALG"""
def __init__(self, arr):
super(tenSortALG, self).__init__()
self.arr = arr
def _bubbleSort(self): #冒泡
n = len(self.arr)
for i in range(n-1):
for j in range(n-1-i):#把最大值冒到最后一位
if self.arr[j]>self.arr[j+1]:
self.arr[j], self.arr[j+1] = self.arr[j+1], self.arr[j]
print(self.arr)
def _selectionSort(self): #选择
n = len(self.arr)
for i in range(n-1):
minIndex = i
for j in range(i+1, n):
if self.arr[j]<self.arr[minIndex]:
minIndex = j
self.arr[i], self.arr[minIndex] = self.arr[minIndex], self.arr[i]
print(self.arr)
def _insertionSort(self): #插入
n = len(self.arr)
for i in range(n-1):
j = i
tmp = self.arr[i+1]
while j>=0 and self.arr[j]>tmp:
self.arr[j+1] = self.arr[j]
j-=1
self.arr[j+1] = tmp
print(self.arr)
def _shell_Sort(self): #shell
n = len(self.arr)
gap = n//2
while gap:
for i in range(gap):#一种逐步缩小步长的插入排序
for j in range(i+gap,n,gap):
tmp = self.arr[j]
preIndex = j-gap
while preIndex>=0 and tmp<self.arr[preIndex]:
self.arr[preIndex+gap] = self.arr[preIndex]
preIndex-=gap
self.arr[preIndex+gap] = tmp
gap//=2
print(self.arr)
def _partition(self, left, right):
pivot = self.arr[right]
i = left - 1 #哨兵
for j in range(left, right):
if self.arr[j]<=pivot:
i+=1
self.arr[i], self.arr[j] = self.arr[j], self.arr[i]
self.arr[i+1], self.arr[right] = self.arr[right], self.arr[i+1]
return i+1
def _quickSort(self, left, right):
if left<right:
mid = self._partition(left, right)
self._quickSort(left, mid-1)
self._quickSort(mid+1, right)
def _qSort(self): #快排
n = len(self.arr)
self._quickSort(0, n-1)
print(self.arr)
def _merge(self, left, mid, right):
TEMP = []
i = left
j = mid+1
while i<=mid and j<=right:
if self.arr[i]<=self.arr[j]:
TEMP.append(self.arr[i])
i+=1
else:
TEMP.append(self.arr[j])
j+=1
while i<=mid:
TEMP.append(self.arr[i])
i+=1
while j<=right:
TEMP.append(self.arr[j])
j+=1
t = 0
while left<=right:
self.arr[left] = TEMP[t]
left+=1
t+=1
def _mergeSort(self, left, right):
if left<right:
mid = left+right>>1
self._mergeSort(left, mid)
self._mergeSort(mid+1, right)
self._merge(left, mid, right)
def _mSort(self): #归并
n = len(self.arr)
self._mergeSort(0, n-1)
print(self.arr)
def _adjustHeap_dg(self, i, length):#递归调整堆,大根堆用来升序排序
maxIndex = i
if i*2+1<length and self.arr[i*2+1]>self.arr[maxIndex]:#下标从0开始
maxIndex = i*2+1
if i*2+2<length and self.arr[i*2+2]>self.arr[maxIndex]:
maxIndex = i*2+2
if maxIndex != i:
self.arr[maxIndex], self.arr[i] = self.arr[i], self.arr[maxIndex]
self._adjustHeap_dg(maxIndex, length)
def _adjustHeap_ndg(self, i, length):#循环调整堆
maxIndex = i
k = i*2+1 #i的左子节点
while k<length:
if k+1<length and self.arr[k]<self.arr[k+1]:#按大小指向左/右子节点
k+=1
if self.arr[k]>self.arr[maxIndex]:
maxIndex = k
else:
break
k = k*2+1
if maxIndex != i:
self.arr[maxIndex], self.arr[i] = self.arr[i], self.arr[maxIndex]
'''
tmp = self.arr[i]
while k<length:
if k+1<length and self.arr[k]<self.arr[k+1]:#按大小指向左/右子节点
k+=1
if self.arr[k]>tmp:
self.arr[i] = self.arr[k]
i = k
else:
break
k = k*2+1
self.arr[i] = tmp
'''
def _heapSort(self): #堆排序(大根堆)
n = len(self.arr)
for i in range(n//2-1,-1,-1):
self._adjustHeap_ndg(i, n)
#print(self.arr)
#不断交换末尾与堆顶元素,并调整堆,直至有序
for j in range(n-1, 0, -1):
self.arr[0], self.arr[j] = self.arr[j], self.arr[0]
#print(self.arr)
self._adjustHeap_ndg(0, j)
#print(self.arr)
print(self.arr)
def _CountingSort(self): #计数排序
n = len(self.arr)
if n==0:
return
Min = Max = self.arr[0]
for i in range(1, n):
Max = max(Max, self.arr[i])
Min = min(Min, self.arr[i])
bias = 0 - Min
bucket = [0]*(Max-Min+1)
for i in range(n):
bucket[self.arr[i]+bias]+=1
index, i = 0,0
while index<n:
if bucket[i]>0:
self.arr[index] = i-bias
bucket[i]-=1
index+=1
else:
i+=1
print(self.arr)
def _bucketSort(self, bucketSize): #桶排序
n = len(self.arr)
if n<2:
return
Min = Max = self.arr[0]
for i in range(1, n):
Max = max(Max, self.arr[i])
Min = min(Min, self.arr[i])
bucketCount = (Max-Min)//bucketSize +1
bucket = [[] for i in range(bucketCount)]
for i in range(n):
#print((self.arr[i]-Min)//bucketSize)
bucket[(self.arr[i]-Min)//bucketSize].append(self.arr[i])
self.arr.clear()
for i in range(bucketCount):
tlen = len(bucket[i])
bucket[i].sort()
for j in range(tlen):
self.arr.append(bucket[i][j])
print(self.arr)
def _RadixSortSort(self): #基数排序
n = len(self.arr)
if n<2:
return
Max = self.arr[0]
for i in range(1,n):
Max = max(self.arr[i], Max)
#print(Max)
maxDigit = int(math.log(Max,10))+1 #获取位数
#print(maxDigit)
mod, div = 10, 1
bucketList = [[] for _ in range(10)] #
for i in range(maxDigit):
for j in range(n):
num = (self.arr[j]%mod)//div
bucketList[num].append(self.arr[j])
index = 0
for j in range(10):
tlen = len(bucketList[j])
for k in range(tlen):
self.arr[index] = bucketList[j][k]
index+=1
bucketList[j].clear()
mod*=10
div*=10
print(self.arr)
arr = [300,154,4,52,21,6]
ans = tenSortALG(arr)
ans._RadixSortSort()