几种基础排序(python)
很久没写过排序算法了,现在对各种常见的排序进行复习并且记录,以下算法实现有问题的欢迎提出。
class Sort:
num = 0
def __init__(self):
print('sort class')
#快速排序
def quickSort(self,array,start,end):
i = start
j = end
if (j<len(array) and i<len(array) and i != j):
Sort.num += 1
print ('calculate times:%d,start:%d,end:%d'%(Sort.num,start,end))
tmp = 0
isChange = False
while (i != j):
if array[i] > array[j]:
tmp = array[i]
array[i] = array[j]
array[j] = tmp
if isChange:
isChange = False
else:
isChange = True
if isChange:
i+=1
else:
j-=1
self.quickSort(array, start, i)
self.quickSort(array, i+1, end)
#插入排序
def insertSort(self,array):
size = len(array)
Sort.num = 0
for i in range(1,size):
tmp = array[i]
j = i-1
while j >= 0 and array[j] > tmp:
Sort.num += 1
print ('calculate times:%d,changePreIndex:%d,changePostIndex:%d'%(Sort.num,j+1,j))
array[j+1] = array[j]
j -= 1
array[j+1] = tmp
#选择排序
def selectSort(self,array):
size = len(array)
for i in range(0,size):
j = i+1
for k in range(j,size):
if array[i] > array[k]:
tmp = array[i]
array[i] = array[k]
array[k] = tmp
#堆排序
def heapSort(self,array):
size = len(array)
for i in range(0,size-1):
print ('calculate times:%d,heap:%s'%(i+1,array))
self.buildMaxHeap(array,size-1-i)
self.exchange(array,0,size-1-i)
def buildMaxHeap(self,array,lastIndex):
k = (lastIndex - 1) / 2
for i in xrange(k,-1,-1):
while 2*i+1 <= lastIndex:
bigIndex = 2*i +1
if bigIndex + 1 <= lastIndex and array[bigIndex] < array[bigIndex+1]:
bigIndex += 1
if array[i] <= array[bigIndex]:
self.exchange(array,i,bigIndex)
i = bigIndex
else:
break
def exchange(self,array,aIndex,bIndex):
tmp = array[aIndex]
array[aIndex] = array[bIndex]
array[bIndex] = tmp
#计数排序
def countSort(self,array):
max = -1
min = -1
size = len(array)
array1 = array[:]
for i in range(0,size-1):
if array1[i] > array1[i+1]:
max = array1[i]
array1[i] = array1[i+1]
array1[i+1] = max
max = array1[size-1]
print('max:%d'%(max))
for j in range(size-1,0,-1):
if array1[j] < array1[j-1]:
min = array1[j]
array1[j] = array1[j-1]
array1[j-1] = min
min = array1[0]
print('min:%d'%(min))
csize = max-min+1
carray = [0]*csize
darray = [0]*size
for j in range(0,csize):
carray[j] = 0
for k in range(0,size):
carray[array[k]-min] += 1
print carray
for m in range(1,csize):
carray[m] += carray[m-1]
print carray
for l in range(0,size):
darray[carray[array[l]-min] - 1] = array[l]
carray[array[l]-min] -= 1
print darray
def merge(self,array,low,mid,height):
i = low
j = mid + 1
size = height - low + 1
marray = size*[0]
k = 0
while i <= mid and j<= height:
if array[i] > array[j]:
marray[k] = array[j]
k += 1
j += 1
else:
marray[k] = array[i]
k += 1
i += 1
if i > mid:
while j <= height:
marray[k] = array[j]
k += 1
j += 1
if j > height:
while i <= mid:
marray[k] = array[i]
k += 1
i += 1
l = 0
while l < size:
array[low + l] = marray[l]
l += 1
#归并排序
def mergeSort(self,array,low,height):
mid = (height + low) / 2
if height > low:
self.mergeSort(array,low,mid)
self.mergeSort(array,mid+1,height)
self.merge(array,low,mid,height)
#基数排序
def radixSort(self,array):
max = array[0]
size = len(array)
print('size:%d'%(size))
for i in range(1,size):
if array[i] > max:
max = array[i]
print('max:%d'%(max))
time = 0
while max > 0 :
max = max / 10
time += 1
print('time:%d'%(time))
countArray = [0]*10
numberArray = [[0 for col in range(size)] for row in range(10)]
n = 1
while time > 0:
for j in range(0,size):
x = (array[j] / n) % 10
numberArray[x][countArray[x]] = array[j]
countArray[x] += 1
print('numberArray:%s'%(numberArray))
print('countArray:%s'%(countArray))
y = 0
for k in range(0,10):
if countArray[k] > 0:
l = countArray[k]
for p in range(0,l):
array[y] = numberArray[k][p]
y += 1
countArray[k] = 0
n *= 10
time -= 1
#希尔排序
def shellSort(self,array):
time = 0
size = len(array)
gap = size / 2
while gap > 0:
for i in range(gap,size):
j = i - gap
while j >= 0 and array[j] > array[j + gap]:
time += 1
print('time:%d'%(time))
tmp = array[j]
array[j] = array[j+gap]
array[j+gap] = tmp
j -= gap
gap /= 2
if __name__ == '__main__':
sort = Sort()
# heights1 = [2,1,5,6,2,3,4,6,6,2,1]
# lastIndex = len(heights1) - 1
# sort.quickSort(heights1, 0, lastIndex)
# print heights1
# print 'sort.num=%d'%sort.num
# heights2 = [11,10,9,8,7,6,5,4,3,2,1]
# sort.insertSort(heights2)
# print heights2
# heights3 = [2,1,5,6,2,3,4,6,6,2,1]
# sort.heapSort(heights3)
# heights4 = [2,1,5,6,2,3,4,6,6,2,1]
# sort.countSort(heights4)
# heights5 = [2,1,5,6,2,3,4,6,6,2,1]
# h = len(heights5) - 1
# sort.mergeSort(heights5,0,h)
# print heights5
# heights6 = [22,31,35,16,62,73,84,36,66,92,10]
# sort.radixSort(heights6)
# print(heights6)
# heights7 = [22,31,35,16,62,73,84,36,66,92,10]
# sort.selectSort(heights7)
# print(heights7)
heights7 = [22,31,35,16,62,73,84,36,66,92,10]
sort.shellSort(heights7)
print(heights7)