From 6007e52b99e9940901fb47553a8a18bbde07b22f Mon Sep 17 00:00:00 2001 From: ryanneilparker Date: Thu, 6 Oct 2022 10:19:01 +0000 Subject: [PATCH 1/4] Add binary insertion sort --- sorting/binary_insertion_sort.rb | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 sorting/binary_insertion_sort.rb diff --git a/sorting/binary_insertion_sort.rb b/sorting/binary_insertion_sort.rb new file mode 100644 index 0000000..68e403c --- /dev/null +++ b/sorting/binary_insertion_sort.rb @@ -0,0 +1,51 @@ +# Ruby implementation +# of binary insertion sort algorithm + + + +require 'pry' + +def binary_search(arr, val, start, stop) + while start <= stop + + mid = (start + stop) / 2 + + if val == arr[mid] # val is in the middle + return mid + elsif val > arr[mid] # val is on the right side + start = mid + 1 + else + stop = mid - 1 # val is on the left side + end + end + + start +end + +def binary_insertion_sort(arr) + n = arr.size + + (0...n).each do |index| + j = index - 1 + selected = arr[index] + + # find location where selected value should be inserted + location = binary_search(arr, selected, 0, j) + + # move all elements after location to make space + while j >= location + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = selected + end + end + + arr +end + +if $0 == __FILE__ + puts 'Enter a list of numbers separated by space' + + list = gets.split.map(&:to_i) + p binary_insertion_sort(list) +end \ No newline at end of file From c32ffeccb06acd667891d98b2d9ba7c7c29a67d9 Mon Sep 17 00:00:00 2001 From: ryanneilparker Date: Thu, 6 Oct 2022 10:22:24 +0000 Subject: [PATCH 2/4] Add binary_insertion_sort.rb --- sorting/binary_insertion_sort.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sorting/binary_insertion_sort.rb b/sorting/binary_insertion_sort.rb index 68e403c..c487464 100644 --- a/sorting/binary_insertion_sort.rb +++ b/sorting/binary_insertion_sort.rb @@ -1,9 +1,4 @@ -# Ruby implementation -# of binary insertion sort algorithm - - - -require 'pry' +# Ruby implementation of binary insertion sort algorithm def binary_search(arr, val, start, stop) while start <= stop From e927c673fa82a30a9a526987b7b59cd7a5a5e814 Mon Sep 17 00:00:00 2001 From: ryanneilparker Date: Thu, 6 Oct 2022 10:26:34 +0000 Subject: [PATCH 3/4] Add binary_insertion_sort_test.rb --- sorting/binary_insertion_sort_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 sorting/binary_insertion_sort_test.rb diff --git a/sorting/binary_insertion_sort_test.rb b/sorting/binary_insertion_sort_test.rb new file mode 100644 index 0000000..68594e1 --- /dev/null +++ b/sorting/binary_insertion_sort_test.rb @@ -0,0 +1,11 @@ +require 'minitest/autorun' +require_relative './sort_tests' +require_relative './binary_insertion_sort' + +class TestBubbleSort < Minitest::Test + include SortTests + + def sort(input) + binary_insertion_sort(input) + end +end \ No newline at end of file From f2fdc233bfc1b36b376bb6ce990b2f4c1ffaefac Mon Sep 17 00:00:00 2001 From: ryanneilparker Date: Thu, 6 Oct 2022 10:29:43 +0000 Subject: [PATCH 4/4] Correct test class --- sorting/binary_insertion_sort_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/binary_insertion_sort_test.rb b/sorting/binary_insertion_sort_test.rb index 68594e1..7b83d4b 100644 --- a/sorting/binary_insertion_sort_test.rb +++ b/sorting/binary_insertion_sort_test.rb @@ -2,7 +2,7 @@ require 'minitest/autorun' require_relative './sort_tests' require_relative './binary_insertion_sort' -class TestBubbleSort < Minitest::Test +class TestBinaryInsertionSort < Minitest::Test include SortTests def sort(input)