Skip to content
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

added Selection Sort, Bubble Sort and Factorial to python Algoritm #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions python/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Bubble Sort Algorithm

https://en.wikipedia.org/wiki/Bubble_sort
'''

def bubble_sort(arr):
length = len(arr)

for i in range(length):
for j in range(1,length-i):
if arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]


if __name__ == '__main__':
arr = [5, 10, 2, 0, 78, -2]
bubble_sort(arr)
print(arr)

'''
output: [-2, 0, 2, 5, 10, 78]
'''
25 changes: 25 additions & 0 deletions python/factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Program to calculate factorial of given number

>>>factorial(1)
1

>>>factorial(10)
3628800

'''
def factorial(num):
if num < 0:
print("Enter positive number")
elif num == 0:
return 1
else:
ans = 1
for i in range(1, num+1):
ans *= i

return ans

if __name__ == '__main__':
num = int(input())
print(factorial(num))
25 changes: 25 additions & 0 deletions python/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Selection sort algorithm implimented in Python
for more info on selection sort visit:
https://en.wikipedia.org/wiki/Selection_sort
'''

def selection_sort(arr):
for i in range(len(arr)):
minimum = i

for j in range(i+1, len(arr)):
if arr[j] < arr[minimum]:
minimum = j;

if minimum != i:
arr[i], arr[minimum] = arr[minimum], arr[i]

if __name__ == '__main__':
arr = [5, 10, 2, 0, 78, -2]
selection_sort(arr)
print(arr)

'''
output: [-2, 0, 2, 5, 10, 78]
'''