-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mohamedfarhan-m/mohamedfarhan-m-patch-1
Create bubsort.py
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,23 @@ | ||
def bubble_sort(arr): | ||
|
||
# Outer loop to iterate through the list n times | ||
for n in range(len(arr) - 1, 0, -1): | ||
|
||
# Inner loop to compare adjacent elements | ||
for i in range(n): | ||
if arr[i] > arr[i + 1]: | ||
|
||
# Swap elements if they are in the wrong order | ||
swapped = True | ||
arr[i], arr[i + 1] = arr[i + 1], arr[i] | ||
|
||
|
||
# Sample list to be sorted | ||
arr = [39, 12, 18, 85, 72, 10, 2, 18] | ||
print("Unsorted list is:") | ||
print(arr) | ||
|
||
bubble_sort(arr) | ||
|
||
print("Sorted list is:") | ||
print(arr) |