mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-02-05 08:46:12 +01:00
Merge pull request #86 from vzvu3k6k/sort_tests
Complete tests of sort algorithms
This commit is contained in:
commit
11328ae839
16 changed files with 137 additions and 25 deletions
|
@ -82,11 +82,19 @@
|
|||
* [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)
|
||||
* [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)
|
||||
* [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)
|
||||
* [Radix Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/radix_sort_test.rb)
|
||||
* [Selection Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/selection_sort.rb)
|
||||
* [Selection Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/selection_sort_test.rb)
|
||||
* [Shell Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/shell_sort.rb)
|
||||
* [Shell Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/shell_sort_test.rb)
|
||||
* [Sort Tests](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/sort_tests.rb)
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
DEFAULT_BUCKET_SIZE = 5
|
||||
|
||||
def bucket_sort(input, bucket_size = DEFAULT_BUCKET_SIZE)
|
||||
print 'Array is empty' if input.empty?
|
||||
|
||||
array = input.split(' ').map(&:to_i)
|
||||
|
||||
def bucket_sort(array, bucket_size = DEFAULT_BUCKET_SIZE)
|
||||
bucket_count = ((array.max - array.min) / bucket_size).floor + 1
|
||||
|
||||
# create buckets
|
||||
|
@ -21,9 +17,12 @@ def bucket_sort(input, bucket_size = DEFAULT_BUCKET_SIZE)
|
|||
bucket.sort!
|
||||
end
|
||||
|
||||
buckets.flatten.join(' ')
|
||||
buckets.flatten
|
||||
end
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets
|
||||
print bucket_sort(list)
|
||||
if $0 == __FILE__
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets.split.map(&:to_i)
|
||||
p bucket_sort(list)
|
||||
end
|
||||
|
|
11
sorting/bucket_sort_test.rb
Normal file
11
sorting/bucket_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './bucket_sort'
|
||||
|
||||
class TestBucketSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
bucket_sort(input)
|
||||
end
|
||||
end
|
|
@ -27,6 +27,9 @@ def adjusted_down(adjusted_array, parent, limit)
|
|||
adjusted_array[parent] = top
|
||||
end
|
||||
|
||||
# Code for testing heapsort
|
||||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].shuffle
|
||||
print heap_sort(array)
|
||||
if $0 == __FILE__
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets.split.map(&:to_i)
|
||||
p heap_sort(list)
|
||||
end
|
||||
|
|
11
sorting/heap_sort_test.rb
Normal file
11
sorting/heap_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './heap_sort'
|
||||
|
||||
class TestHeapSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
heap_sort(input)
|
||||
end
|
||||
end
|
|
@ -10,8 +10,10 @@ def insertion_sort(array)
|
|||
end
|
||||
array
|
||||
end
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets
|
||||
insertion_sort(list)
|
||||
print list
|
||||
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/insertion_sort_test.rb
Normal file
11
sorting/insertion_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './insertion_sort'
|
||||
|
||||
class TestInsertionSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
insertion_sort(input)
|
||||
end
|
||||
end
|
|
@ -28,6 +28,9 @@ def merge_sort(array)
|
|||
result
|
||||
end
|
||||
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
list = gets
|
||||
print merge_sort list.split(' ').map(&:to_i)
|
||||
if $0 == __FILE__
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets.split.map(&:to_i)
|
||||
p merge_sort(list)
|
||||
end
|
||||
|
|
11
sorting/merge_sort_test.rb
Normal file
11
sorting/merge_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './merge_sort'
|
||||
|
||||
class TestMergeSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
merge_sort(input)
|
||||
end
|
||||
end
|
|
@ -11,5 +11,9 @@ def quicksort(arr)
|
|||
[*quicksort(left), pivot, *quicksort(right)]
|
||||
end
|
||||
|
||||
arr = [34, 2, 1, 5, 3]
|
||||
p quicksort(arr)
|
||||
if $0 == __FILE__
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets.split.map(&:to_i)
|
||||
p quicksort(list)
|
||||
end
|
||||
|
|
11
sorting/quicksort_test.rb
Normal file
11
sorting/quicksort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './quicksort'
|
||||
|
||||
class TestQuicksort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
quicksort(input)
|
||||
end
|
||||
end
|
11
sorting/radix_sort_test.rb
Normal file
11
sorting/radix_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './radix_sort'
|
||||
|
||||
class TestRadixSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
radix_sort(input)
|
||||
end
|
||||
end
|
|
@ -11,8 +11,12 @@ def selection_sort(array)
|
|||
array[i], array[smallest] = array[smallest], array[i] if i != smallest
|
||||
i += 1
|
||||
end
|
||||
array
|
||||
end
|
||||
|
||||
arr = [9, 8, 3, 1, 2, 55, 68, 48].shuffle # We have taken a rondom example and also shuffling it
|
||||
selection_sort(arr)
|
||||
puts "Sorted array is: #{arr.inspect}"
|
||||
if $0 == __FILE__
|
||||
puts 'Enter a list of numbers separated by space'
|
||||
|
||||
list = gets.split.map(&:to_i)
|
||||
p selection_sort(list)
|
||||
end
|
||||
|
|
11
sorting/selection_sort_test.rb
Normal file
11
sorting/selection_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './selection_sort'
|
||||
|
||||
class TestSelectionSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
selection_sort(input)
|
||||
end
|
||||
end
|
11
sorting/shell_sort_test.rb
Normal file
11
sorting/shell_sort_test.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'minitest/autorun'
|
||||
require_relative './sort_tests'
|
||||
require_relative './shell_sort'
|
||||
|
||||
class TestShellSort < Minitest::Test
|
||||
include SortTests
|
||||
|
||||
def sort(input)
|
||||
shell_sort(input)
|
||||
end
|
||||
end
|
|
@ -5,7 +5,8 @@
|
|||
# # SortTests adds some test_* methods.
|
||||
# include SortTests
|
||||
#
|
||||
# # SortTests requires sort method.
|
||||
# # SortTests requires sort method which receives an array of integers
|
||||
# # and returns a sorted one.
|
||||
# def sort(input)
|
||||
# input.sort
|
||||
# end
|
||||
|
|
Loading…
Add table
Reference in a new issue