Skip to content

Commit dd2cef0

Browse files
committed
bubble_sort.py
horners_method_polynomial.py multiplication_algorithm.py
1 parent 31a6c6d commit dd2cef0

3 files changed

+117
-0
lines changed

bubble_sort.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
bubble sorting
3+
get the size of the array
4+
set a while loop that checks the array size is larger than zero (for every successful for loop, reduce size by 1
5+
because after first loop and subsequent loops, the largest numbers will be sorted permanently)
6+
for loop between 0 and the size of the array-1 (as you cannot check the last array value against nothing)
7+
swapping adjacent values if a is larger than b
8+
original = O(N^2)
9+
10+
improvement:
11+
if no swapping as occured, break loop
12+
13+
'''
14+
15+
16+
def bubble_sorting(array):
17+
n = len(array)
18+
19+
while n > 0:
20+
swapped = False
21+
for i in range(0, n - 1):
22+
if array[i] > array[i + 1]:
23+
array[i], array[i + 1] = array[i + 1], array[i]
24+
swapped = True
25+
if not swapped:
26+
print(n)
27+
break
28+
n -= 1
29+
return array
30+
31+
32+
array_test = [100, 0, 4, 2, 6, 8, 2, 4, 11, 10]
33+
34+
print(bubble_sorting(array_test))

horners_method_polynomial.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
horner's method
3+
4+
cnxn + cn-1xn-1 + cn-2xn-2 + … + c1x + c0
5+
f(x)=k=0∑n​ak​xk f(x) = sum[a(k) * x^(k)] where k = 0 and nth degree,
6+
i.e. deg(f(x)) = n where n represents the highest variable exponent. so if I had the values of a= [4,3,2] and x = 2
7+
8+
f(2) = [4 * 2^0] + [3 * 2^1] + [2 * 2^2]
9+
f(2) = 18
10+
11+
1. Set k = n
12+
2. Let bk = ak
13+
3. Let bk - 1 = ak - 1 + bkx0
14+
4. Let k = k - 1
15+
5. If k ≥ 0 then go to step 3
16+
Else End
17+
18+
another way to look at it
19+
20+
f(x) = 2x^2 + 3x + 4
21+
= (x(2x + 3)) + 4
22+
23+
24+
'''
25+
26+
27+
def horner(a, x):
28+
n = len(a)
29+
p = a[0]
30+
31+
for i in range(1, n):
32+
p = p * x + a[i]
33+
34+
return p
35+
36+
37+
a = [2, 3, 4]
38+
x = 2
39+
# 2x^2 + 3x + 4
40+
# x= 2
41+
# 8 + 6 + 4
42+
print(horner(a, x))
43+
44+
45+
def polynomial_long_division(a, x):
46+
result = [a[0]]
47+
for i in range(1, len(a)):
48+
result.append(a[i] + (x * result[i - 1]))
49+
return result, print(f'The remainder is {result[-1]}')
50+
51+
52+
# f(x) = 2x**3 - 6x**2 + 2x - 1, x = 3
53+
# x - 3 = 0
54+
f = [2, -6, 2, -1]
55+
print(polynomial_long_division(f, 3))

multiplication_algorithm.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
3+
Shift method
4+
5+
c is the base and must be bigger than or equal to 2 - c>= 2
6+
7+
When c is the base of your representation (like decimal), then this is how multiplication can be done "manually".
8+
It's the "shift and add" method. In c-base cy is a single shift of y to the left (i.e. adding a zero at the right);
9+
and z/c is a single shift of z to the right: the right most digit is lost. That lost digit is actually z mod c,
10+
which is multiplied with y separately.
11+
12+
Here is an example with c = 10, where the apostrophe signifies the value of variables in a recursive call. We perform
13+
the multiplication with y for each separate digit of z (retrieved with z mod c). Each next product found in this way
14+
is written shifted one more place to the left. Usually the 0 is not padded at the right of this shifted product,
15+
but it is silently assumed:
16+
17+
'''
18+
import math
19+
20+
21+
def multiply(y, z, c):
22+
if z == 0 or y == 0:
23+
return 0
24+
else:
25+
return multiply(c * y, math.floor((z / c)), c) + y * (z % c)
26+
27+
28+
print(multiply(10, 20, 2))

0 commit comments

Comments
 (0)