#-*- coding: UTF-8 -*-
import numpy as np
from QuickSort import QuickSort
def BucketSort(a, n):
barrel = {}
for i in xrange(0,n):
barrel.setdefault(i, [])
min = np.min(a)
max = np.max(a)
for x in a:
for i in xrange(0,n-1):
if x >= min +i* (max - min)/n and x < min +(i +1) * (max - min)/n:
barrel[i].append(x)
elif i == n-2 and x >= min +(i +1) * (max - min)/n:
barrel[i+1].append(x)
k = 0
for i in xrange(0,n):
if len(barrel[i]) != 0:
arr = np.array(barrel[i])
QuickSort(arr, 0, len(barrel[i]) -1)
for x in arr:
a[k] = x
k += 1
if __name__ == '__main__':
a = np.random.randint(0, 100, size = 10)
print "Before sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"
BucketSort(a, 10)
print "After sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"

#-*- coding: UTF-8 -*-
import numpy as np
def Partition(a, i, j):
x = a[i] #将数组的第一个元素作为初始基准位置
p = i #同时记录下该元素的位置
while i < j:
while i < j and a[j] >= x:
j -= 1
while i < j and a[i] <= x:
i += 1
if i != j:
a[i], a[j] = a[j], a[i] #交换a[i]与a[j]
a, a[i] = a[i], a #将a与a[i]进行交换
p = i #得到分隔位置
return p
def QuickSort(a, i, j):
if i < j:
p = Partition(a, i, j)
QuickSort(a, i, p-1)
QuickSort (a, p+1, j)
if __name__ == '__main__':
a = np.random.randint(0, 100, size = 100)
print "Before sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"
QuickSort(a, 0, a.size - 1)
print "After sorting..."
print "---------------------------------------------------------------"
print a
print "---------------------------------------------------------------"

tags: print,Python,min,barrel,QuickSort,lt,max,np,sorting,排序,gt

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。