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

pr-6 #6

wants to merge 1 commit into from

Conversation

CheezItMan
Copy link

This method sorts an array


index = 0
swapped = true
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

@@ -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

swapped = true
while index < array.length && swapped
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.

Copy link

@sophearychiv sophearychiv left a comment

Choose a reason for hiding this comment

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

The time complexity is O(n^2). Can we sort it with O(nlgn)?

while index < array.length && swapped
swapped = false
other_index = index + 1
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

Copy link

@ChubbyCub ChubbyCub left a comment

Choose a reason for hiding this comment

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

Good job on checking for nil and empty array and array with one element.
See comments for other potential exceptions in your code.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants