Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pr-6 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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)

Choose a reason for hiding this comment

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

This method of sorting has a time complexity of O(n^2). You might be able to improve it to have O(nlogn) time complexity by implementing quicksort

if array == nil || array.length <= 1
return array # nothing to sort
end

index = 0
swapped = true
Copy link

@gracemshea gracemshea Aug 15, 2019

Choose a reason for hiding this comment

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

will this work without swapped? advise remove this second condition if possible

while index < array.length && swapped
Copy link

@ChubbyCub ChubbyCub Aug 15, 2019

Choose a reason for hiding this comment

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

Will there be an array out of bound exception right here since other_index always moves one step ahead of index? Maybe the boundary for index should only be array.length - 1

swapped = false
other_index = index + 1
Copy link

@ChubbyCub ChubbyCub Aug 15, 2019

Choose a reason for hiding this comment

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

other_index is not a meaningful name. You can name it adjacent_index.

while other_index < array.length

Choose a reason for hiding this comment

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

Boundary for other_index might be wrong. it might be array.length - index - 1

if array[index] > array[other_index]
temp = array[index]
array[index] = array[other_index]
array[other_index] = temp
swapped = true
end
other_index += 1
end
index += 1
end

return array
end