Skip to content
Browse files

more plots

  • Loading branch information...
1 parent 1b50d54 commit 8901940873aa2623a193d4bb9c33c97b5d0c836a Lorenzo Bolla committed May 21, 2012
Showing with 18 additions and 9 deletions.
  1. +4 −3 google/bench.py
  2. +7 −3 google/bucket_sort.py
  3. +5 −3 google/plot.gp
  4. +2 −0 google/radix_sort.py
View
7 google/bench.py
@@ -1,7 +1,4 @@
# TODO
-# heapsort
-# mergesort
-# radixsort
# bucketsort
import random
@@ -13,6 +10,8 @@
from mergesort import mergesort
from heap import heapsort
from counting_sort import counting_sort
+from radix_sort import radix_sort
+from bucket_sort import bucket_sort
def csort(v):
@@ -27,6 +26,8 @@ def main():
('mergesort', mergesort, False),
('qsort', lambda v: qsort(v, 0, len(v) - 1), True),
('counting_sort', csort, False),
+ ('radix_sort', radix_sort, False),
+ ('bucket_sort', bucket_sort, False),
]
res = {}
View
10 google/bucket_sort.py
@@ -7,11 +7,15 @@ def get_bucket(x, nb):
def bucket_sort(v):
- nb = 10
+ if not v:
+ return v
+ nb = max(10, len(v) / 10)
b = [[] for _ in xrange(nb)]
+ m = max(v) * 1.01
for x in v:
- b[get_bucket(x, nb)].append(x)
+ b[get_bucket(x / m, nb)].append(x)
res = []
+ # print min([len(x) for x in b]), max([len(x) for x in b])
for i in b:
insertion_sort(i)
res += i
@@ -20,5 +24,5 @@ def bucket_sort(v):
if __name__ == '__main__':
for _ in xrange(10):
- v = [random.random() for _ in xrange(20)]
+ v = [random.random() for _ in xrange(2000)]
assert bucket_sort(v) == sorted(v)
View
8 google/plot.gp
@@ -7,11 +7,13 @@ set style line 4 lt 2 lc rgb "green" lw 2
set key top left
-plot "n.csv" with points ls 1 title "O(n)", \
+plot "n2.csv" with points ls 3 title "O(n^2)", \
"n_log_n.csv" with points ls 2 title "O(n lg n)", \
- "n2.csv" with points ls 3 title "O(n^2)", \
+ "n.csv" with points ls 1 title "O(n)", \
"insertionsort.csv" with l ls 3 title "insertionsort", \
"heapsort.csv" with l ls 2 title "heapsort", \
"mergesort.csv" with l ls 2 title "mergesort", \
"qsort.csv" with l ls 2 title "qsort", \
- "counting_sort.csv" with l ls 1 title "countingsort"
+ "counting_sort.csv" with l ls 1 title "countingsort", \
+ "radix_sort.csv" with l ls 1 title "radixsort", \
+ "bucket_sort.csv" with l ls 1 title "bucketsort"
View
2 google/radix_sort.py
@@ -28,6 +28,8 @@ def counting_sort(a, n, k, key):
def radix_sort(v):
+ if not v:
+ return v
ds = num_digits(max(v))
for d in xrange(ds):
v = counting_sort(v, len(v), 10, lambda x: get_digit(x, d + 1))

0 comments on commit 8901940

Please sign in to comment.
Something went wrong with that request. Please try again.