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 some math algorithms #436

Open
wants to merge 3 commits into
base: main
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
21 changes: 21 additions & 0 deletions alphabetical-sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# alphabetical-sorting.py
# ----------------------------------------------------
words = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]

# using the sorted() function
print(sorted(words))

# using the sort() method
words_sorted = words
words_sorted.sort()
print(words_sorted)

# using the reverse=True parameter
print(sorted(words, reverse=True))

# using the bubble sort
for i in range(len(words)):
for j in range(len(words) - 1):
if words[j] > words[j + 1]:
words[j], words[j + 1] = words[j + 1], words[j]
print(words)
52 changes: 52 additions & 0 deletions bucket-sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# bucket sorting mechanism

data = [5, 3, 1, 2, 4, 6, 7, 8, 9, 10]


def bucket_sorting(arr):
# get the maximum value from the array
max_value = max(arr)
# create a bucket with the size of max_value + 1
bucket = [0] * (max_value + 1)
# loop through the array
for i in arr:
# increment the value of the bucket at the index of the current value of the array
bucket[i] += 1
# create a new array
new_arr = []
# loop through the bucket
for i in range(len(bucket)):
# loop through the value of the bucket at the current index
for j in range(bucket[i]):
# append the index to the new array
new_arr.append(i)
# return the new array
return new_arr


print(bucket_sorting(data))


# bucket sorting algorithm for float numbers

def bucket_sorting_float(arr):
# create a new array
new_arr = []
# loop through the array
for i in range(len(arr)):
# append the value of the array at the current index to the new array
new_arr.append(arr[i])
# loop through the new array
for i in range(len(new_arr)):
# loop through the new array
for j in range(len(new_arr)):
# if the value of the new array at the current index is greater than the value of the new array at the
# next index
if new_arr[i] < new_arr[j]:
# swap the values
new_arr[i], new_arr[j] = new_arr[j], new_arr[i]
# return the new array
return new_arr


print(bucket_sorting_float([0.1, 0.2, 0.3, 0.69, 0.5, 0.34]))
20 changes: 20 additions & 0 deletions zero-points-with-bisection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# bisection method for finding zero points of a function


def f(x):
return -4 * x + 2


def bisection(a, b, eps):
while (b - a) > eps:
c = (a + b) / 2
if f(c) == 0:
return c
elif f(a) * f(c) < 0:
b = c
else:
a = c
return (a + b) / 2


print(bisection(0, 1, 0.0001))