Update bubble_sort.rs #894
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sorting function
The file implements the bubble_sort function for sorting using the bubble sort algorithm.
The function takes a modifiable reference to a slice of elements and sorts it in ascending order.
If the array is empty, the function returns control immediately.
Inside the loop, the array is traversed, neighboring elements are compared, and if the current one is larger than the next, they are swapped.
If there were no permutations during the pass, the array is considered sorted and the loop is terminated.
After each complete pass, the range for the next iteration is decreased by 1 because the last element is already in its place.
Tests
The tests module implements tests for various cases:
For each test it is checked that the result is really sorted and contains the same elements as the initial array (using the auxiliary functions is_sorted and have_same_elements).