Last active
September 13, 2022 12:39
-
-
Save hoffm/9087fcbe2da8f8566f2bb901656fda57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include Quicksort | |
RSpec.describe 'Quicksort' do | |
describe '#partition!' do | |
it 'partitions the p..r subarray around a pivot' do | |
array = [9, 7, 5, 11, 12, 2, 14, 3, 10, 4, 6] | |
pivot = partition!(array, 0, array.length - 1) | |
expect(pivot).to eq(4) | |
expect(array).to eq([5, 2, 3, 4, 6, 7, 14, 9, 10, 11, 12]) | |
end | |
it 'handles incomplete subarrays' do | |
array = [9, 7, 5, 11, 12, 2, 14, 3, 10, 4, 6] | |
pivot = partition!(array, 0, 2) | |
expect(pivot).to eq(0) | |
expect(array).to eq([5, 7, 9, 11, 12, 2, 14, 3, 10, 4, 6]) | |
end | |
it 'handles zeros and negatives' do | |
array = [0, -1, 2, 0] | |
pivot = partition!(array, 0, array.length - 1) | |
expect(pivot).to eq(2) | |
expect(array).to eq([0, -1, 0, 2]) | |
end | |
end | |
describe '#quicksort!' do | |
it 'sorts the specified subarray' do | |
array = [9, 7, 5, 11, 12, 2, 14, 3, 10, 4, 6] | |
quicksort!(array, 0, array.length - 1) | |
expect(array).to eq(array.sort) | |
end | |
it 'handles incomplete subarrays' do | |
array = [9, 7, 5, 11, 12, 2, 14, 3, 10, 4, 6] | |
quicksort!(array, 0, 2) | |
expect(array).to eq([5, 7, 9, 11, 12, 2, 14, 3, 10, 4, 6]) | |
end | |
it 'handles zeros and negatives' do | |
array = [0, -1, 2, 0] | |
quicksort!(array, 0, array.length - 1) | |
expect(array).to eq([-1, 0, 0, 2]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment