Created
February 19, 2016 21:57
-
-
Save lmaresz/4fa3c5eac6b7b1bbf070 to your computer and use it in GitHub Desktop.
First manual quickshort - duplacated elements dont work :(
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
from random import randint | |
import time | |
array = [x for x in range(1000000,0,-1)] | |
start_time = time.time() | |
def Quickshort(array): | |
pivot_i = randint(0, len(array)-1) | |
pivot = array[pivot_i] | |
left_i = 0 | |
right_i = len(array)-1 | |
while (right_i != left_i and left_i < len(array)): | |
if array[left_i] >= pivot: | |
while (right_i > -1): | |
if array[right_i] > pivot: | |
right_i -= 1 | |
else: | |
array[left_i],array[right_i] = array[right_i],array[left_i] | |
break | |
else: | |
left_i += 1 | |
pivot_i = left_i | |
if len(array[:pivot_i]) > 1: array[:pivot_i] = Quickshort(array[:pivot_i]) | |
if len(array[pivot_i:]) > 1: array[pivot_i:] = Quickshort(array[pivot_i:]) | |
return array | |
Quickshort(array) | |
print("DONE elapsed time: %.2fs" % float(time.time() - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment