forked from hhhrrrttt222111/CodeChef
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hhhrrrttt222111#258 bubble sort and modified bubble sort in python
- Loading branch information
1 parent
ec40036
commit 224ed7d
Showing
2 changed files
with
34 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,16 @@ | ||
def bubbleSort(arr): | ||
n = len(arr) | ||
ctr = 0 | ||
for i in range(n-1): | ||
for j in range(0, n-i-1): | ||
if arr[j] > arr[j + 1] : | ||
arr[j], arr[j + 1] = arr[j + 1], arr[j] | ||
ctr += 1 | ||
print(ctr) | ||
|
||
|
||
lst = [1, 2, 3, 5, 4] | ||
bubbleSort(lst) | ||
# since lists are mutable | ||
# the main list itself changes | ||
print(lst) |
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,18 @@ | ||
def bubbleSort(arr): | ||
n = len(arr) | ||
ctr = 0 | ||
for i in range(n): | ||
swapped = False | ||
for j in range(0, n-i-1): | ||
if arr[j] > arr[j+1] : | ||
arr[j], arr[j+1] = arr[j+1], arr[j] | ||
swapped = True | ||
if swapped == False: | ||
break | ||
|
||
|
||
lst = [1, 2, 3, 5, 4] | ||
bubbleSort(lst) | ||
# since lists are mutable | ||
# the main list itself changes | ||
print(lst) |