Module: Algorithmable::Sort::Utils
Instance Method Summary collapse
- #exchange(from, to, collection) ⇒ Object
-
#partition(a, bottom, top) ⇒ Integer
Integer representing new pivot location.
-
#swap(collection, i) ⇒ Array
Collection.
Instance Method Details
#exchange(from, to, collection) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/algorithmable/sort/utils.rb', line 47 def exchange(from, to, collection) return collection if from == to local_from = collection[from] collection[from] = collection[to] collection[to] = local_from collection end |
#partition(a, bottom, top) ⇒ Integer
Returns Integer representing new pivot location.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/algorithmable/sort/utils.rb', line 8 def partition(a, bottom, top) i = bottom j = top.succ v = a[bottom] loop do while a[i += 1] < v break if i == top end while v < a[j -= 1] break if j == bottom end break if i >= j cur_i = a[i] a[i] = a[j] a[j] = cur_i end cur_bottom = a[bottom] a[bottom] = a[j] a[j] = cur_bottom j end |
#swap(collection, i) ⇒ Array
Returns collection.
58 59 60 61 62 63 |
# File 'lib/algorithmable/sort/utils.rb', line 58 def swap(collection, i) current = collection[i] collection[i] = collection[i + 1] collection[i + 1] = current collection end |