Created
November 11, 2019 17:10
-
-
Save abdellani/53897bfee15c9cb6f373cb919af96e97 to your computer and use it in GitHub Desktop.
This file contains 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
def getIndex(string) | |
string.split(" ").first.to_i | |
end | |
def getString(string) | |
string.split(" ").last | |
end | |
def fms(array) | |
return array if array.length==1 | |
first_subarray_limit=(array.length.to_f/2).floor | |
second_subarray_limit=(array.length.to_f/2).ceil | |
first_subarray_limit-=1 if first_subarray_limit == second_subarray_limit | |
array1=fms(array[0..first_subarray_limit]) | |
array2=fms(array[second_subarray_limit..array.length]) | |
sorted_array=[] | |
while not array1.empty? or not array2.empty? | |
sorted_array<< array1.shift if (array2.empty? and not array1.empty?) or (not array1.empty? and getIndex(array1.first)<=getIndex(array2.first)) | |
sorted_array<< array2.shift if (array1.empty? and not array2.empty?) or (not array2.empty? and getIndex(array1.first)>getIndex(array2.first)) | |
end | |
sorted_array | |
end | |
def full_merge_sort(array) | |
# write your code here | |
array=fms(array) | |
result=[] | |
array.each do |element| | |
result<< getString(element) | |
end | |
result | |
end | |
full_merge_sort(["0 ab", "6 cd", "0 ef", "6 gh", "4 ij", "0 ab", "6 cd", "0 ef", "6 gh", "0 ij", "4 that", "3 be", "0 to", "1 be", "5 question", "1 or", "2 not", "4 is", "2 to", "4 the"]) | |
# => ["ab", "ef", "ab", "ef", "ij", "to", "be", "or", "not", "to", "be", "ij", "that", "is", "the", "question", "cd", "gh", "cd", "gh"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment