首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 软件考试 > 初级职称 >

快速排序算法:Python

2010-08-18 
读书人IT频道reader8.com/exam/jisuanji/  研究Python中,写一个快速排序练练手:  ‘‘‘  qsort.py  Quick sort  Created on Jun 18, 2009  @author: Liao  ‘‘‘  from random import Random  def quick_sort(arr):  if l
读书人IT频道reader8.com/exam/jisuanji/   研究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 读书人IT频道reader8.com/exam/jisuanji/
热点排行