Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
teste
1 change: 1 addition & 0 deletions lib/sorting_algorithms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions lib/sorting_algorithms/quick_sort.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 1 addition & 16 deletions spec/sorting_algorithms/bubble_sort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 1 addition & 16 deletions spec/sorting_algorithms/insertion_sort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 1 addition & 16 deletions spec/sorting_algorithms/merge_sort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions spec/sorting_algorithms/quick_sort_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe(SortingAlgorithms::QuickSort) do
it_behaves_like "a sorting algorithm"
end
17 changes: 1 addition & 16 deletions spec/sorting_algorithms/selection_sort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions spec/support/shared_examples/sorting_algorithms.rb
Original file line number Diff line number Diff line change
@@ -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