diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e69de29..6f16acb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -0,0 +1 @@ +teste diff --git a/lib/sorting_algorithms.rb b/lib/sorting_algorithms.rb index 6852b42..bf01ee9 100644 --- a/lib/sorting_algorithms.rb +++ b/lib/sorting_algorithms.rb @@ -4,6 +4,7 @@ require_relative "sorting_algorithms/insertion_sort" require_relative "sorting_algorithms/selection_sort" require_relative "sorting_algorithms/merge_sort" +require_relative "sorting_algorithms/quick_sort" module SortingAlgorithms end diff --git a/lib/sorting_algorithms/quick_sort.rb b/lib/sorting_algorithms/quick_sort.rb new file mode 100644 index 0000000..af475f4 --- /dev/null +++ b/lib/sorting_algorithms/quick_sort.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module SortingAlgorithms + module QuickSort + class << self + def call(array) + return array if array.length <= 1 + + pivot = array.last + left = [] + right = [] + + array[0...-1].each do |element| + if element <= pivot + left << element + + else + right << element + end + end + + call(left) + [pivot] + call(right) + end + end + end +end diff --git a/spec/sorting_algorithms/bubble_sort_spec.rb b/spec/sorting_algorithms/bubble_sort_spec.rb index 75e047f..3ce8cce 100644 --- a/spec/sorting_algorithms/bubble_sort_spec.rb +++ b/spec/sorting_algorithms/bubble_sort_spec.rb @@ -3,20 +3,5 @@ require "spec_helper" RSpec.describe(SortingAlgorithms::BubbleSort) do - describe ".call" do - it "returns an ascending sorted array" do - expect(described_class.call([10, 4, 8, 3, 11])) - .to(eq([3, 4, 8, 10, 11])) - end - - it "returns an ascending sorted array with repeating numbers" do - expect(described_class.call([10, 10, 8, 8, 11])) - .to(eq([8, 8, 10, 10, 11])) - end - - it "returns an ascending sorted array with zeros and negative numbers" do - expect(described_class.call([0, -1, -20, 8, 11])) - .to(eq([-20, -1, 0, 8, 11])) - end - end + it_behaves_like "a sorting algorithm" end diff --git a/spec/sorting_algorithms/insertion_sort_spec.rb b/spec/sorting_algorithms/insertion_sort_spec.rb index 0e71b92..06739e6 100644 --- a/spec/sorting_algorithms/insertion_sort_spec.rb +++ b/spec/sorting_algorithms/insertion_sort_spec.rb @@ -3,20 +3,5 @@ require "spec_helper" RSpec.describe(SortingAlgorithms::InsertionSort) do - describe ".call" do - it "returns an ascending sorted array" do - expect(described_class.call([10, 4, 8, 3, 11])) - .to(eq([3, 4, 8, 10, 11])) - end - - it "returns an ascending sorted array with repeating numbers" do - expect(described_class.call([10, 10, 8, 8, 11])) - .to(eq([8, 8, 10, 10, 11])) - end - - it "returns an ascending sorted array with zeros and negative numbers" do - expect(described_class.call([0, -1, -20, 8, 11])) - .to(eq([-20, -1, 0, 8, 11])) - end - end + it_behaves_like "a sorting algorithm" end diff --git a/spec/sorting_algorithms/merge_sort_spec.rb b/spec/sorting_algorithms/merge_sort_spec.rb index 67594e4..84b51a0 100644 --- a/spec/sorting_algorithms/merge_sort_spec.rb +++ b/spec/sorting_algorithms/merge_sort_spec.rb @@ -3,20 +3,5 @@ require "spec_helper" RSpec.describe(SortingAlgorithms::MergeSort) do - describe ".call" do - it "return an ascending sorted array" do - expect(described_class.call([10, 4, 8, 3, 11])) - .to(eq([3, 4, 8, 10, 11])) - end - - it "return an ascending sorted array with repeating numbers" do - expect(described_class.call([10, 10, 8, 8, 11])) - .to(eq([8, 8, 10, 10, 11])) - end - - it "returns an ascending sorted array with zeros and negative numbers" do - expect(described_class.call([0, -1, -20, 8, 11])) - .to(eq([-20, -1, 0, 8, 11])) - end - end + it_behaves_like "a sorting algorithm" end diff --git a/spec/sorting_algorithms/quick_sort_spec.rb b/spec/sorting_algorithms/quick_sort_spec.rb new file mode 100644 index 0000000..20c7b77 --- /dev/null +++ b/spec/sorting_algorithms/quick_sort_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe(SortingAlgorithms::QuickSort) do + it_behaves_like "a sorting algorithm" +end diff --git a/spec/sorting_algorithms/selection_sort_spec.rb b/spec/sorting_algorithms/selection_sort_spec.rb index 5da8407..9bed3fa 100644 --- a/spec/sorting_algorithms/selection_sort_spec.rb +++ b/spec/sorting_algorithms/selection_sort_spec.rb @@ -3,20 +3,5 @@ require "spec_helper" RSpec.describe(SortingAlgorithms::SelectionSort) do - describe ".call" do - it "return an ascending sorted array" do - expect(described_class.call([10, 4, 8, 3, 11])) - .to(eq([3, 4, 8, 10, 11])) - end - - it "return an ascending sorted array with repeating numbers" do - expect(described_class.call([10, 10, 8, 8, 11])) - .to(eq([8, 8, 10, 10, 11])) - end - - it "returns an ascending sorted array with zeros and negative numbers" do - expect(described_class.call([0, -1, -20, 8, 11])) - .to(eq([-20, -1, 0, 8, 11])) - end - end + it_behaves_like "a sorting algorithm" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f0e5ec3..0b36bd3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,4 +23,6 @@ # Executa os testes em ordem aleatória para detectar dependências ocultas config.order = :random Kernel.srand(config.seed) + + Dir[File.join(__dir__, "support/**/*.rb")].sort.each { |f| require f } end diff --git a/spec/support/shared_examples/sorting_algorithms.rb b/spec/support/shared_examples/sorting_algorithms.rb new file mode 100644 index 0000000..4a04174 --- /dev/null +++ b/spec/support/shared_examples/sorting_algorithms.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +RSpec.shared_examples("a sorting algorithm") do + describe ".call" do + it "returns an ascending sorted array" do + expect(described_class.call([10, 4, 8, 3, 11])) + .to(eq([3, 4, 8, 10, 11])) + end + + it "returns an ascending sorted array with repeating numbers" do + expect(described_class.call([10, 10, 8, 8, 11])) + .to(eq([8, 8, 10, 10, 11])) + end + + it "returns an ascending sorted array with zeros and negative numbers" do + expect(described_class.call([0, -1, -20, 8, 11])) + .to(eq([-20, -1, 0, 8, 11])) + end + end +end