首页 > 代码库 > hdu 1284 钱币兑换问题
hdu 1284 钱币兑换问题
# -*- coding: utf-8 -*- """ Created on Fri May 16 17:24:05 2014 @author: lifeix """ #快速排序 import sys import random length = 30 def qsort(arr,left,right): lp = left rp = right if lp == rp:return while True: while arr[lp] >= arr[right] and rp > lp: lp = lp +1 while arr[rp] <= arr[right] and rp > lp: rp = rp - 1 arr[lp],arr[rp] = arr[rp],arr[lp] if lp >= rp: break arr[rp],arr[right] = arr[right],arr[lp] if left < lp: qsort(arr,left,lp - 1) qsort(arr,rp,right) def main(): arr = [] sys.setrecursionlimit(100000) for i in range(length): arr.append(random.randint(0,10000)) qsort(arr,0,length-1) print arr if __name__ == ‘__main__‘: for i in range(10): main()
#快速排序第二种实现 def quickSort(arr,p,r): if p < r: q = partition(arr,p,r) quickSort(arr,p,q - 1) quickSort(arr,q+1,r) def partition(arr,p,r): x = arr[r] i = p for j in range(p,r): if arr[j] < x: arr[i],arr[j] = arr[j],arr[i] i = i + 1 arr[i],arr[r] = arr[r],arr[i] return i if __name__ == ‘__main__‘: arr = [1,3,89,2,0,78,98,23,56,100] quickSort(arr,0,len(arr) - 1) print arr
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。