Created
July 27, 2017 10:13
-
-
Save chankeypathak/5b3aac39237d6fd6063857961fc78d6f to your computer and use it in GitHub Desktop.
Sorting a million 32-bit integers in 2MB of RAM using Python
This file contains hidden or 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
#!/usr/bin/env python3.0 | |
import sys, array, tempfile, heapq | |
assert array.array('i').itemsize == 4 | |
def intsfromfile(f): | |
while True: | |
a = array.array('i') | |
a.fromstring(f.read(4000)) | |
if not a: | |
break | |
for x in a: | |
yield x | |
iters = [] | |
while True: | |
a = array.array('i') | |
a.fromstring(sys.stdin.buffer.read(40000)) | |
if not a: | |
break | |
f = tempfile.TemporaryFile() | |
array.array('i', sorted(a)).tofile(f) | |
f.seek(0) | |
iters.append(intsfromfile(f)) | |
a = array.array('i') | |
for x in heapq.merge(*iters): | |
a.append(x) | |
if len(a) >= 1000: | |
a.tofile(sys.stdout.buffer) | |
del a[:] | |
if a: | |
a.tofile(sys.stdout.buffer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: http://neopythonic.blogspot.in/2008/10/sorting-million-32-bit-integers-in-2mb.html