Conversation
| while other_index < array.length | ||
| if array[index] > array[other_index] | ||
| temp = array[index] | ||
| array[index] = array[other_index] |
There was a problem hiding this comment.
Consider renaming. Index / Other_index can get a little confusing.
| @@ -0,0 +1,25 @@ | |||
|
|
|||
| def sort(array) | |||
| if array == nil || array.length <= 1 | |||
| if array[index] > array[other_index] | ||
| temp = array[index] | ||
| array[index] = array[other_index] | ||
| array[other_index] = temp | ||
| swapped = true |
| array[index] = array[other_index] | ||
| array[other_index] = temp | ||
| swapped = true | ||
| end |
There was a problem hiding this comment.
This is a bit confusing here, could we add some clarifying comments?
| other_index = index + 1 | ||
| while other_index < array.length | ||
| if array[index] > array[other_index] | ||
| temp = array[index] |
There was a problem hiding this comment.
Consider using a more descriptive variable name than "temp"
|
|
||
| def sort(array) | ||
| if array == nil || array.length <= 1 | ||
| return array # nothing to sort |
There was a problem hiding this comment.
should it return nil if there is no array?
| index = 0 | ||
| swapped = true | ||
| 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.
Bubble sort? A few comments might be helpful for those of us who don't understand what this is doing at first glance.
| swapped = true | ||
| while index < array.length && swapped | ||
| swapped = false | ||
| other_index = index + 1 |
There was a problem hiding this comment.
maybe since it's the next index the variable should be called next_index instead of other_index
| array[index] = array[other_index] | ||
| array[other_index] = temp | ||
| swapped = true | ||
| end |
There was a problem hiding this comment.
maybe parallel assignment would make this more readable instead of using temp
| @@ -0,0 +1,25 @@ | |||
|
|
|||
| def sort(array) | |||
There was a problem hiding this comment.
a comment describing the sorting algorithm could be helpful for comprehension
| other_index = index + 1 | ||
| while other_index < array.length | ||
| if array[index] > array[other_index] | ||
| temp = array[index] |
There was a problem hiding this comment.
Can you rename temp to a more descriptive variable?
This method sorts an array