Skip to content
Open
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
25 changes: 25 additions & 0 deletions sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

def sort(array)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a comment describing the sorting algorithm could be helpful for comprehension

if array == nil || array.length <= 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice base case!

return array # nothing to sort

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it return nil if there is no array?

end

index = 0
swapped = true
while index < array.length && swapped
swapped = false
other_index = index + 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe since it's the next index the variable should be called next_index instead of other_index

while other_index < array.length
if array[index] > array[other_index]
temp = array[index]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name than "temp"

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename temp to a more descriptive variable?

array[index] = array[other_index]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming. Index / Other_index can get a little confusing.

array[other_index] = temp
swapped = true
Comment on lines +13 to +17

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great use of temp variable here

end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing here, could we add some clarifying comments?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe parallel assignment would make this more readable instead of using temp

other_index += 1
end
index += 1
end
Comment on lines +7 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bubble sort? A few comments might be helpful for those of us who don't understand what this is doing at first glance.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree


return array
end