array-sort 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +73 -0
  3. data/README.md +228 -29
  4. data/lib/array/sort.rb +5 -8
  5. data/lib/array_sort/algorithms/binary_insertion_sort.rb +41 -0
  6. data/lib/array_sort/algorithms/bubble_sort.rb +31 -0
  7. data/lib/array_sort/algorithms/bucket_sort.rb +67 -0
  8. data/lib/array_sort/algorithms/cocktail_shaker_sort.rb +42 -0
  9. data/lib/array_sort/algorithms/comb_sort.rb +33 -0
  10. data/lib/array_sort/algorithms/counting_sort.rb +50 -0
  11. data/lib/array_sort/algorithms/cycle_sort.rb +46 -0
  12. data/lib/array_sort/algorithms/gnome_sort.rb +28 -0
  13. data/lib/array_sort/algorithms/heap_sort.rb +43 -0
  14. data/lib/array_sort/algorithms/insertion_sort.rb +35 -0
  15. data/lib/array_sort/algorithms/intro_sort.rb +80 -0
  16. data/lib/array_sort/algorithms/merge_sort.rb +57 -0
  17. data/lib/array_sort/algorithms/odd_even_sort.rb +34 -0
  18. data/lib/array_sort/algorithms/pancake_sort.rb +39 -0
  19. data/lib/array_sort/algorithms/quick_sort.rb +68 -0
  20. data/lib/array_sort/algorithms/radix_sort.rb +58 -0
  21. data/lib/array_sort/algorithms/selection_sort.rb +25 -0
  22. data/lib/array_sort/algorithms/shell_sort.rb +41 -0
  23. data/lib/array_sort/algorithms/smooth_sort.rb +106 -0
  24. data/lib/array_sort/algorithms/tim_sort.rb +335 -0
  25. data/lib/array_sort/algorithms/tournament_sort.rb +53 -0
  26. data/lib/array_sort/algorithms/tree_sort.rb +96 -0
  27. data/lib/array_sort/array_methods.rb +67 -0
  28. data/lib/array_sort/core.rb +124 -0
  29. data/lib/array_sort/functions.rb +23 -0
  30. data/lib/array_sort/refinements.rb +16 -0
  31. data/lib/array_sort/registry.rb +68 -0
  32. data/lib/array_sort/trace.rb +111 -0
  33. data/lib/{array/sort → array_sort}/version.rb +1 -1
  34. data/lib/array_sort.rb +43 -0
  35. data/sig/array_sort.rbs +368 -0
  36. metadata +47 -71
  37. data/.gitignore +0 -9
  38. data/.rubocop.yml +0 -11
  39. data/.travis.yml +0 -6
  40. data/Gemfile +0 -6
  41. data/Gemfile.lock +0 -22
  42. data/Rakefile +0 -10
  43. data/array-sort.gemspec +0 -37
  44. data/bin/console +0 -14
  45. data/bin/setup +0 -8
  46. data/lib/array/sort/bubble_sort.rb +0 -91
  47. data/lib/array/sort/heap_sort.rb +0 -111
  48. data/lib/array/sort/helper.rb +0 -34
  49. data/lib/array/sort/insertion_sort.rb +0 -78
  50. data/lib/array/sort/merge_sort.rb +0 -94
  51. data/lib/array/sort/quick_sort.rb +0 -73
@@ -1,94 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Array
4
- # Returns a new array created by sorting +self+, using merge sort algorithm.
5
- #
6
- # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
- #
8
- # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
- # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
- #
11
- # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
12
- # will be preserved.
13
- #
14
- # @return [Array] the sorted array
15
- def merge_sort(&block)
16
- return dup if length <= 1
17
- divided_parts = merge_sort_divide
18
- part_a_sorted = divided_parts[0].merge_sort
19
- part_b_sorted = divided_parts[1].merge_sort
20
- merge_sort_merge part_a_sorted, part_b_sorted, &block
21
- end
22
-
23
- # Sorts +self+ in place, using merge sort algorithm.
24
- #
25
- # Comparisons for the sort will be done using the <=> operator or using an optional code block.
26
- #
27
- # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
28
- # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
29
- #
30
- # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
31
- # will be preserved.
32
- #
33
- # @return [Array] +self+
34
- def merge_sort!(&block)
35
- become_clone_of merge_sort(&block)
36
- end
37
-
38
- # Returns a new array created by sorting +self+ with merge sort algorithm, using a set of keys generated by mapping
39
- # the values in self through the given block.
40
- #
41
- # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
42
- # will be preserved.
43
- #
44
- # If no block is given, an Enumerator is returned instead.
45
- #
46
- # @return [Array] if a block is given, the sorted array
47
- # @return [Enumerator] if no block is given, an Enumerator
48
- def merge_sort_by(&_block)
49
- if block_given?
50
- merge_sort do |a, b|
51
- yield(a) <=> yield(b)
52
- end
53
- else
54
- to_enum :merge_sort_by
55
- end
56
- end
57
-
58
- # Sorts +self+ in place with merge sort algorithm, using a set of keys generated by mapping the values in self through
59
- # the given block.
60
- #
61
- # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
62
- # will be preserved.
63
- #
64
- # If no block is given, an Enumerator is returned instead.
65
- #
66
- # @return [Array] if a block is given, the sorted array
67
- # @return [Enumerator] if no block is given, an Enumerator
68
- def merge_sort_by!(&block)
69
- if block_given?
70
- become_clone_of merge_sort_by(&block)
71
- else
72
- to_enum :merge_sort_by!
73
- end
74
- end
75
-
76
- private
77
-
78
- # @private
79
- def merge_sort_divide
80
- mid = length / 2
81
- part_a = self[0, mid]
82
- part_b = self[mid..-1]
83
- [part_a, part_b]
84
- end
85
-
86
- # @private
87
- def merge_sort_merge(part_a, part_b, &block)
88
- result = []
89
- while part_a.length.positive? && part_b.length.positive?
90
- result << (sort_compare(part_a.first, part_b.first, &block).positive? ? part_b : part_a).shift
91
- end
92
- result + part_a + part_b
93
- end
94
- end
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Array
4
- # Returns a new array created by sorting +self+, using quicksort algorithm.
5
- #
6
- # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
- #
8
- # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
- # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
- #
11
- # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
12
- # elements is unpredictable.
13
- #
14
- # @return [Array] the sorted array
15
- def quick_sort(&block)
16
- return dup if length <= 1
17
- left, right = self[1..-1].partition { |y| sort_compare(first, y, &block).positive? }
18
- left.quick_sort + [first] + right.quick_sort
19
- end
20
-
21
- # Sorts +self+ in place, using quicksort algorithm.
22
- #
23
- # Comparisons for the sort will be done using the <=> operator or using an optional code block.
24
- #
25
- # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
26
- # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
27
- #
28
- # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
29
- # elements is unpredictable.
30
- #
31
- # @return [Array] +self+
32
- def quick_sort!(&block)
33
- become_clone_of quick_sort(&block)
34
- end
35
-
36
- # Returns a new array created by sorting +self+ with quicksort algorithm, using a set of keys generated by mapping
37
- # the values in self through the given block.
38
- #
39
- # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
40
- # elements is unpredictable.
41
- #
42
- # If no block is given, an Enumerator is returned instead.
43
- #
44
- # @return [Array] if a block is given, the sorted array
45
- # @return [Enumerator] if no block is given, an Enumerator
46
- def quick_sort_by(&_block)
47
- if block_given?
48
- quick_sort do |a, b|
49
- yield(a) <=> yield(b)
50
- end
51
- else
52
- to_enum :quick_sort_by
53
- end
54
- end
55
-
56
- # Sorts +self+ in place with quicksort algorithm, using a set of keys generated by mapping the values in self through
57
- # the given block.
58
- #
59
- # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
60
- # elements is unpredictable.
61
- #
62
- # If no block is given, an Enumerator is returned instead.
63
- #
64
- # @return [Array] if a block is given, the sorted array
65
- # @return [Enumerator] if no block is given, an Enumerator
66
- def quick_sort_by!(&block)
67
- if block_given?
68
- become_clone_of quick_sort_by(&block)
69
- else
70
- to_enum :quick_sort_by!
71
- end
72
- end
73
- end