Skip to content
This repository was archived by the owner on Jan 20, 2021. It is now read-only.

Commit bbfcad1

Browse files
committed
[v0.3.26] Refactored bubble sort into common
- Move algorithms into `common` folder Signed-off-by: Kenneth Ham <[email protected]>
1 parent b440ae3 commit bbfcad1

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

lib/common/algorithms.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module NUSBotgram
2+
class Algorithms
3+
def initialize
4+
end
5+
6+
public
7+
8+
def bubble_sort(container)
9+
loop do
10+
swapped = false
11+
(container.size - 1).times do |i|
12+
if (container[i] <=> container[i + 1]) == 1
13+
container[i], container[i + 1] = container[i + 1], container[i] # Swap
14+
swapped = true
15+
end
16+
end
17+
break unless swapped
18+
end
19+
20+
container
21+
end
22+
end
23+
end

lib/engine/core.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,5 @@ def check_daytime(time)
291291
return 2
292292
end
293293
end
294-
295-
public
296-
297-
def bubble_sort(container)
298-
loop do
299-
swapped = false
300-
(container.size - 1).times do |i|
301-
if (container[i] <=> container[i + 1]) == 1
302-
container[i], container[i + 1] = container[i + 1], container[i] # Swap
303-
swapped = true
304-
end
305-
end
306-
break unless swapped
307-
end
308-
309-
container
310-
end
311294
end
312295
end

lib/model/models.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../config/global'
2+
require_relative '../common/algorithms'
23

34
module NUSBotgram
45
class Models
@@ -241,6 +242,7 @@ def today_star_command(bot, engine, message, lesson_type, sticker_collections)
241242
public
242243

243244
def predict_next_class(telegram_id, bot, engine, current_time_now, day_today, message, sticker_collections)
245+
algorithms = NUSBotgram::Algorithms.new
244246
module_results = engine.get_mod(telegram_id)
245247

246248
mods_hash = Hash.new
@@ -264,7 +266,7 @@ def predict_next_class(telegram_id, bot, engine, current_time_now, day_today, me
264266
end
265267

266268
# Sort array in ascending order - Time relative
267-
sorted = engine.bubble_sort(time_ary)
269+
sorted = algorithms.bubble_sort(time_ary)
268270

269271
# Process and store the sorted time into Hash
270272
for j in 0...mods_hash.size do

lib/nusbotgram/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module NUSBotgram
2-
VERSION = "0.3.25"
2+
VERSION = "0.3.26"
33
end

0 commit comments

Comments
 (0)