forked from CoderAcademy-BRI/ruby-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_sort_numbers.rb
More file actions
43 lines (40 loc) · 1.23 KB
/
Copy path21_sort_numbers.rb
File metadata and controls
43 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Sorting algorithms are a key tool in programming.
#
# Write an algorithm that will sort an array of numbers
# in ascending order.
# Do not use the .sort method - the idea is that you write
# your own sort method.
#
# Use hand simulation or pythontutor.com to determine how
# many times your solution visits each element of the array
# in a worse case scenario. If your array has 10 elements for example,
# in a worse case situation does your algorithm examine each element
# once? Twice? More? Less?
#
# This will help you begin to understand order of complexity
# of algorithms.
#
# Optional:
# Look up explanations of common sorting algoithms
# like bubble sort and quick sort. Don't look up code, just look up the
# explanations and see if you can implement one of those.
# Which one is your favourite and why?
#
# Example input: [2,5,4,8,2]
# Expected output: [2,2,4,5,8]
# Helper method, immediately throws false when unsorted pair detected.
def check_sorted(num_array)
(1...num_array.size).each do | i |
if num_array[i] < num_array[(i - 1)]
return false
end
end
true
end
# Extremely dumb sort
def sort(num_array)
until check_sorted(num_array)
num_array.shuffle!
end
num_array
end