Advertisement
kxphotographer

QuickSort

Apr 19th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1.  
  2. #coding: utf-8
  3.  
  4. import random
  5.  
  6. num = ''
  7.  
  8. while not num.isdigit():
  9.     num = raw_input('配列の個数を入力: ')
  10.  
  11. num = int(num)
  12.  
  13. def quicksort(seq):
  14.     if len(seq) < 1:
  15.         return seq
  16.     pivot = seq[0]
  17.     left = []
  18.     right = []
  19.     for x in range(1, len(seq)):
  20.         if seq[x] <= pivot:
  21.             left.append(seq[x])
  22.         else:
  23.             right.append(seq[x])
  24.     left = quicksort(left)
  25.     right = quicksort(right)
  26.  
  27.     return left + [pivot] + right
  28.  
  29. if __name__ == '__main__':
  30.     ary = []
  31.     for n in range(num):
  32.         ary.append(random.randint(1,num*10))
  33.  
  34.     print '\n変換前:'
  35.     print str(ary)
  36.     print '\n変換後:'
  37.     print str(quicksort(ary))
  38.     print ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement