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: sort method #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

PR 6: sort method #6

wants to merge 1 commit into from

Conversation

CheezItMan
Copy link

No description provided.

Comment on lines +3 to +4
if array == nil || array.length <= 1
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.

nice comment

Choose a reason for hiding this comment

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

I second that.

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

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?

Choose a reason for hiding this comment

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

Again, nice call.

Choose a reason for hiding this comment

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

good catch!

Comment on lines +12 to +22
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

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
Copy link

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
Copy link

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

Copy link

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.

Comment on lines +14 to +16
temp = array[index]
array[index] = array[other_index]
array[other_index] = temp

Choose a reason for hiding this comment

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

Suggested change
temp = array[index]
array[index] = array[other_index]
array[other_index] = temp
array[index], array[other_index] = array[other_index], array[index]

Comment on lines +9 to +22
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

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

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?

Comment on lines +3 to +5
if array == nil || array.length <= 1
return array # nothing to sort
end

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.

Comment on lines +14 to +16
temp = array[index]
array[index] = array[other_index]
array[other_index] = temp

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]

Copy link

@anyatokar anyatokar 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! 👯‍♂️


def sort(array)
if array == nil || array.length <= 1
return array # nothing to sort
Copy link

@anyatokar anyatokar Mar 12, 2021

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

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

Copy link

@marks214 marks214 left a 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
Copy link

@MonaRahmani MonaRahmani Mar 11, 2021

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?

Comment on lines +15 to +16
array[index] = array[other_index]
array[other_index] = temp

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]

Comment on lines +9 to +10
while index < array.length && swapped
swapped = false

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?

Choose a reason for hiding this comment

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

good observation

Comment on lines +3 to +5
if array == nil || array.length <= 1
return array # nothing to sort
end

Choose a reason for hiding this comment

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

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

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.