python实现的一种排序方法

在处理数据比较多的时候,适用的一种排序方法




#!/usr/bin/python
# coding:utf-8

class SQList:
	def __init__(self,list=None):
		self.r=list
		
	def swap(self,i,j):
		temp=self.r[i]
		self.r[i]=self.r[j]
		self.r[j]=temp
		
	def heap_sort(self):
		length =len(self.r)
		i=int(length/2)
		
		while(i>=0):
			print ("in the heap_sort, length=%d , i =%d" % (length,i))
			self.heap_adjust(i,length-1)
			i -=1
			
		
		j=length-1
		while j>0:
			print ("in the heap_sort, length=%d , j=%d" % (length,j))
			self.swap(0,j)
			self.heap_adjust(0,j-1)
			j -=1
			
	def heap_adjust(self,s,m):
		list=self.r
		temp=list[s]
		i = 2*s
		print ("in the heap_adjust, s=%d , m =%d i =%d" % (s,m,i))
		while i<=m:
			if i<m and list[i]< list[i+1]:
				i +=1
			if temp >=list[i]:
				break
			list[s]=list[i]
			s=i
			i *=2
		list[s]=temp
		print("after i=%d, the list is %s" % (i,self.r))
	def __str__(self):
		ret=""
		for i in self.r:
			ret +="	 %s" % i
		return ret
		
if __name__ == '__main__':
	sqlist = SQList([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
	print(sqlist)
	sqlist.heap_sort()
	print(sqlist)
	


posted @ 2017-09-05 19:59  枫奇丶宛南  阅读(24)  评论(0编辑  收藏  举报