-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
feat(sorts): add bubble sort algorithm for beginners #11641
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f103a0
feat(sorts): add bubble sort algorithm for beginners
laysaalves 1349ecd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ea06a9b
Merge branch 'TheAlgorithms:master' into master
laysaalves f214a0f
feat(sorts): add bubble sort for beginners with doctests
laysaalves 8aea97a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import List | ||
|
||
|
||
def bubble_sort(nums: List[int]) -> List[int]: | ||
Check failure on line 4 in sorts/simple_bubble_sort.py
|
||
""" | ||
param nums: the array of integers(int) because this application does not use decimal numbers, but if needed, use float type. | ||
:return: the same nums List ordered by ascending | ||
|
||
step 01: create the nums array | ||
step 02: collect the dimension of array | ||
step 03: defines the number of times your array will be traversed according to the number of elements | ||
step 04: for each pair of elements in the array, starting from the Index, reorder them. | ||
Elements already in the correct order will not be modified in the Loop -> (0, n - 1 - i) | ||
step 05: the numbers are compared to arrive at the required order | ||
step 06: the nuns list is returned in ascending order! :) | ||
""" | ||
n = len(nums) # 02 | ||
for i in range(n): # 03 | ||
for j in range(0, n - 1 - i): # 04 | ||
if nums[j] > nums[j + 1]: # 05 | ||
nums[j], nums[j + 1] = nums[j + 1], nums[j] | ||
return nums | ||
|
||
|
||
nums = [65, 66, 12, 4, 9, 10, 32, 2] # 01 | ||
ordered_nums = bubble_sort(nums) # 06 | ||
print("The result:", ordered_nums) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.