Skip to content

Instantly share code, notes, and snippets.

@cdyk
Created March 7, 2020 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdyk/2e22aa7fd761121f085b5e25b6ff8767 to your computer and use it in GitHub Desktop.
Save cdyk/2e22aa7fd761121f085b5e25b6ff8767 to your computer and use it in GitHub Desktop.
Radix sort starting with least significant bit
from itertools import chain
def radixSortLsb(a):
for b in range(0,10):
a = list(chain(filter(lambda x: ((x>>b)&1) == 0, a),
filter(lambda x: ((x>>b)&1) != 0, a)))
return a
ai =[503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703]
radixSortLsb(ai)
# yields
# [61, 87, 154, 170, 275, 426, 503, 509, 512, 612, 653, 677, 703, 765, 897, 908]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment