Permalink
Browse files

order

  • Loading branch information...
1 parent 8901940 commit 0bba6fedcf93fe0fa5e6edd8452cd9fae7ae7726 Lorenzo Bolla committed May 21, 2012
Showing with 24 additions and 0 deletions.
  1. +24 −0 google/order.py
View
@@ -0,0 +1,24 @@
+# from qsort import random_partition as partition
+from qsort import partition as partition
+
+
+def order(A, p, r, i):
+ '''Return the i-th smallest element of A[p:r+1].'''
+ q = partition(A, p, r)
+ k = q - p + 1
+
+ # TODO
+ if i == 0:
+ return A[p]
+
+ if i < k:
+ return order(A, p, q, i)
+ else:
+ return order(A, q + 1, r, i - k)
+
+
+A = [4,6,5,2,1,3,4,5]
+print sorted(A)
+# print order(A, 0, len(A) - 1, 2) # 2
+# print order(A, 0, len(A) - 1, 1) # 1
+print order(A, 0, len(A) - 1, 3) # 3

0 comments on commit 0bba6fe

Please sign in to comment.