Class: Algorithmable::Cups::StacksAndQueues::StackSorter

Inherits:
Object
  • Object
show all
Includes:
DataStructs
Defined in:
lib/algorithmable/cups/stacks_and_queues/stack_sorter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataStructs

#new_bag, #new_deque_queue, #new_fifo_queue, #new_lifo_queue, #new_max_priority_queue, #new_min_priority_queue, #new_ordered_symbol_table, #new_priority_queue

Methods included from DataStructs::Tree

#new_ordered_binary_tree

Methods included from DataStructs::LinkedList

#new_doubly_linked_list, #new_singly_linked_list

Class Method Details

.sort(stack) ⇒ Object


7
8
9
# File 'lib/algorithmable/cups/stacks_and_queues/stack_sorter.rb', line 7

def self.sort(stack)
  new.sort(stack)
end

Instance Method Details

#sort(stack) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/algorithmable/cups/stacks_and_queues/stack_sorter.rb', line 11

def sort(stack)
  local_stack = new_lifo_queue
  until stack.empty?
    temp = stack.pop
    while !local_stack.empty? && local_stack.peek < temp
      stack.push local_stack.pop
    end
    local_stack.push temp
  end
  local_stack
end