Skip to content

Instantly share code, notes, and snippets.

@zhuangya
Created December 29, 2011 06:25
Show Gist options
  • Save zhuangya/1532367 to your computer and use it in GitHub Desktop.
Save zhuangya/1532367 to your computer and use it in GitHub Desktop.
import random
import time
array = []
for i in range(1000000):
array.append(random.randint(1, 100))
def q_sort(nums):
if not nums:
return []
pivort = nums[-1]
prev_list = [item for item in nums if item < pivort]
follow_list = [item for item in nums if item > pivort]
return q_sort(prev_list) + [pivort] * (len(nums) - len(prev_list) - len(follow_list)) + q_sort(follow_list)
def bubblesort(nums):
max_num = max(nums)
others = [ item for item in nums if not item == max_num ]
if others:
return bubblesort(others) + [max_num] * (len(nums) - len(others))
else:
return []
print time.time()
bubblesort(array)
print time.time()
q_sort(array)
print time.time()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment