-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
pr-6 #6
Conversation
|
||
index = 0 | ||
swapped = true | ||
while index < array.length && swapped |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
This method sorts an array