Created
October 2, 2016 13:50
-
-
Save mykhailokrainik/e56e4b9deb1ea5dc0a70e590af2531b6 to your computer and use it in GitHub Desktop.
INSERT SORT ALGORITHM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# INSERT SORT ALGORITHM | |
# _____________________________________________________ | |
# $ ruby insert_sort.rb.rb 1 8 7 2 3 0 -1 8 3 7 6 -1 | |
# insert sort argorithm | |
# original data: [1, 8, 7, 2, 3, 0, -1, 8, 3, 7, 6, -1] | |
# sorted data: [-1, -1, 0, 1, 2, 3, 3, 6, 7, 7, 8, 8] | |
arr = ARGV.map(&:to_i) | |
puts <<DESC | |
insert sort argorithm | |
original data: #{arr} | |
DESC | |
for i in 2...arr.length | |
v = arr[i] | |
j = i - 1 | |
while j >= 0 && arr[j] > v | |
arr[j + 1] = arr[j] | |
j = j - 1 | |
end | |
arr[j + 1] = v | |
end | |
puts <<DESC | |
sorted data: #{arr} | |
DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment