当前所在位置:珠峰网资料 >> 计算机 >> 软件水平 >> 正文
快速排序算法--Python
发布时间:2010/1/13 21:31:38 来源:城市学习网 编辑:海蓝
  研究Python中,写一个快速排序练练手:
  '''
  qsort.py
  Quick sort
  Created on Jun 18, 2009
  @author: Liao
  '''
  from random import Random
  def quick_sort(arr):
  if len(arr) > 1:
  qsort(arr, 0, len(arr) - 1)
  def qsort(arr, start, end):
  base = arr[start]
  pl = start
  pr = end
  while pl < pr:
  while pl < pr and arr[pr] >= base:
  pr -= 1
  if pl == pr:
  break
  else:
  arr[pl], arr[pr] = arr[pr], arr[pl]
  while pl < pr and arr[pl] <= base:
  pl += 1
  if pl == pr:
  break
  else:
  arr[pl], arr[pr] = arr[pr], arr[pl]
  # now pl == pr
  if pl - 1 > start:
  qsort(arr, start, pl - 1)
  if pr + 1 < end:
  qsort(arr, pr + 1, end)
  r = Random()
  a = []
  for i in range(20):
  a.append(r.randint(0, 100))
  print a
  quick_sort(a)
  print a
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved