-
Notifications
You must be signed in to change notification settings - Fork 225
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 #459 from realhunter7869/python-algos
#258 Python algos
- Loading branch information
Showing
3 changed files
with
49 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) |
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,15 @@ | ||
def insertionSort(arr): | ||
for i in range(1, len(arr)): | ||
key = arr[i] | ||
j = i - 1 | ||
while j >= 0 and key < arr[j] : | ||
arr[j + 1] = arr[j] | ||
j -= 1 | ||
arr[j + 1] = key | ||
|
||
|
||
lst = [1, 2, 3, 5, 4] | ||
insertionSort(lst) | ||
# since lists are mutable | ||
# the main list itself changes | ||
print(lst) |