Several sorts added

This commit is contained in:
maxbarsukov 2021-05-06 10:13:25 +03:00
parent 1baf4565fb
commit cb35d346fd
9 changed files with 141 additions and 0 deletions

View file

@ -134,18 +134,26 @@
* [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/ternary_search.rb)
## Sorting
* [Bead Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bead_sort.rb)
* [Bead Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bead_sort_test.rb)
* [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb)
* [Bogo Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort_test.rb)
* [Bubble Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort.rb)
* [Bubble Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort_test.rb)
* [Bucket Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort.rb)
* [Bucket Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort_test.rb)
* [Cocktail Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/cocktail_sort.rb)
* [Cocktail Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/cocktail_sort_test.rb)
* [Comb Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/comb_sort.rb)
* [Comb Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/comb_sort_test.rb)
* [Heap Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort.rb)
* [Heap Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort_test.rb)
* [Insertion Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/insertion_sort.rb)
* [Insertion Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/insertion_sort_test.rb)
* [Merge Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/merge_sort.rb)
* [Merge Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/merge_sort_test.rb)
* [Pancake Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/pancake_sort.rb)
* [Pancake Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/pancake_sort_test.rb)
* [Quicksort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/quicksort.rb)
* [Quicksort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/quicksort_test.rb)
* [Radix Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/radix_sort.rb)

24
sorting/bead_sort.rb Normal file
View file

@ -0,0 +1,24 @@
class Array
def columns
x = map(&:length).max
Array.new(x) do |row|
Array.new(length) { |column| self[column][row] }.compact
end
end
end
def bead_sort(array)
array
.map { |element| [1] * element }
.columns
.columns
.map(&:length)
.reverse
end
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p bead_sort(list)
end

11
sorting/bead_sort_test.rb Normal file
View file

@ -0,0 +1,11 @@
require 'minitest/autorun'
require_relative './sort_tests'
require_relative './bead_sort'
class TestInsertionSort < Minitest::Test
include SortTests
def sort(input)
bead_sort(input)
end
end

24
sorting/cocktail_sort.rb Normal file
View file

@ -0,0 +1,24 @@
def cocktail_sort(array)
start = 0
finish = array.length - 1
way = 1
loop do
swapped = false
start.step(finish - way, way) do |i|
if (array[i] <=> array[i + way]) == way
array[i], array[i + way] = array[i + way], array[i]
swapped = i
end
end
break unless swapped
start, finish, way = swapped, start, -way
end
array
end
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p cocktail_sort(list)
end

View file

@ -0,0 +1,12 @@
require 'minitest/autorun'
require_relative './sort_tests'
require_relative './cocktail_sort'
class TestInsertionSort < Minitest::Test
include SortTests
def sort(input)
cocktail_sort(input)
end
end

22
sorting/comb_sort.rb Normal file
View file

@ -0,0 +1,22 @@
def comb_sort(array)
gap = array.length
swaps = true
while gap > 1 or swaps
gap = [1, (gap / 1.25).to_i].max
swaps = false
0.upto(array.length - gap - 1) do |i|
if array[i] > array[i + gap]
array[i], array[i + gap] = array[i + gap], array[i]
swaps = true
end
end
end
array
end
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p insertion_sort(list)
end

11
sorting/comb_sort_test.rb Normal file
View file

@ -0,0 +1,11 @@
require 'minitest/autorun'
require_relative './sort_tests'
require_relative './comb_sort'
class TestInsertionSort < Minitest::Test
include SortTests
def sort(input)
comb_sort(input)
end
end

18
sorting/pancake_sort.rb Normal file
View file

@ -0,0 +1,18 @@
def pancake_sort(array)
return array if array.length <= 1
(array.length - 1).downto(1) do |index|
max_index = array[0..index].index(array[0..index].max)
next if max_index == index
array[0..max_index] = array[0..max_index].reverse if max_index > 0
array[0..index] = array[0..index].reverse
end
array
end
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p pancake_sort(list)
end

View file

@ -0,0 +1,11 @@
require 'minitest/autorun'
require_relative './sort_tests'
require_relative './pancake_sort'
class TestInsertionSort < Minitest::Test
include SortTests
def sort(input)
pancake_sort(input)
end
end