-
Notifications
You must be signed in to change notification settings - Fork 5
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: sort method #6
base: master
Are you sure you want to change the base?
Conversation
if array == nil || array.length <= 1 | ||
return array # nothing to sort |
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.
nice comment
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.
I second that.
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.
Thirded
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.
is there a better variable name for other_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.
Again, nice call.
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 catch!
while other_index < array.length | ||
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 |
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.
looks good
def sort(array) | ||
if array == nil || array.length <= 1 | ||
return array # nothing to sort | ||
end |
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.
method indentation is misaligned. end can go back one space.
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.
swapped is an extra variable that is being tracked in this method. the method will still be viable without the swapped variable.
end | ||
index += 1 | ||
end | ||
|
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.
naming convention can be improved. index and other indexes to refer to index position are vague. left and right, or first and last could work better.
temp = array[index] | ||
array[index] = array[other_index] | ||
array[other_index] = temp |
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.
temp = array[index] | |
array[index] = array[other_index] | |
array[other_index] = temp | |
array[index], array[other_index] = array[other_index], array[index] |
while index < array.length && swapped | ||
swapped = false | ||
other_index = index + 1 | ||
while other_index < array.length | ||
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 |
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.
Consider reducing the time complexity of O(n^2).
@@ -0,0 +1,25 @@ | |||
|
|||
def sort(array) | |||
if array == nil || array.length <= 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.
You can also do array.nil?
if array == nil || array.length <= 1 | ||
return array # nothing to sort | ||
end |
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.
Consider re-writing this as a postfix conditional.
temp = array[index] | ||
array[index] = array[other_index] | ||
array[other_index] = temp |
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.
have you considered parallel assignment?
array[index], array[other_index] = array[other_index], array[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.
good job! 👯♂️
|
||
def sort(array) | ||
if array == nil || array.length <= 1 | ||
return array # nothing to sort |
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.
consider: catching the error that results from the array containing string and number elements. meaning, this edge case throws an error ["sfsf", 324234]
|
||
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.
if possible, consider reducing the O(n^2) time complexity
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.
Overall, looks good. Sorting algorithm is working. Nice job handling different array lengths.
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.
consider change the nested while loop?
array[index] = array[other_index] | ||
array[other_index] = temp |
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.
consider parallel assignment: array[index], array[other_index] = array[other_index], array[index]
while index < array.length && swapped | ||
swapped = false |
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.
Are you sure this "swapped" guard is working as corrected? I believe it could exit this loop prematurely if array[index] and array[other_index] didn't need to be swapped (since the "true" is only set in the if statement). Have you written tests for this loop?
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 observation
if array == nil || array.length <= 1 | ||
return array # nothing to sort | ||
end |
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.
if array == nil || array.length <= 1 | |
return array # nothing to sort | |
end | |
return array if array == nil || array.length <= 1 |
No description provided.