Created
December 29, 2011 06:25
-
-
Save zhuangya/1532367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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