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
def mergesort(L): | |
return L if len(L) < 2 else merge(mergesort(L[:len(L)/2]), mergesort(L[len(L)/2:])) | |
def merge(A,B): | |
Out = [] | |
iA = iB = 0 | |
while len(A)+len(B) > iA + iB: | |
if len(B) <= iB or (len(A) > iA and A[iA] < B[iB]): | |
Out.append(A[iA]) | |
iA = iA + 1 |
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 | |
R = random.Random(42) | |
def qsort(L): | |
if len(L) < 2: return L | |
i = R.randrange(len(L)) # Just some random index in the list. | |
return qsort([x for x in L if x < L[i]]) + [x for x in L if x == L[i]] + qsort([x for x in L if x > L[i]]) |
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
#include <queue> | |
int select_digit (int number, int place) | |
{ return number/place % 10; } | |
/* | |
Create an array of 10 buckets, each storing an empty queue (use ArrayQueue) | |
for every place (6 digit numbers: 1s, 10s, 100s, 1,000s, 10,000s, and 100,000s) | |
for every value in array a |
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
// Declare/initialize any useful variables here, including the temporary array | |
// to merge values into and then copy back into the parameter array. | |
/* | |
while the temporary array is not filled | |
if there are no more values in the left part of a, | |
move next value from right part of a into next index of the temporary array otherwise, if there are no more values in the right part of a, | |
move next value from left part of a into next index of the temporary array otherwise (values in each part) compare the first value in each part | |
move smallest value from a into next index of the temporary array |
NewerOlder