From 4ce3ace5baef305862a2962994287eedaf02312b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Mruga=C5=82a?= <66689589+kris007iron@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:16:54 +0000 Subject: [PATCH 1/3] bucket sorting algorithm --- bucket-sorting.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 bucket-sorting.py diff --git a/bucket-sorting.py b/bucket-sorting.py new file mode 100644 index 00000000..1da70e1b --- /dev/null +++ b/bucket-sorting.py @@ -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])) \ No newline at end of file From c5d2f8e54783c57eace21f7339d1dd28b6836371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Mruga=C5=82a?= <66689589+kris007iron@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:18:25 +0000 Subject: [PATCH 2/3] name of file ;) --- zero-points-with-bisection.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 zero-points-with-bisection.py diff --git a/zero-points-with-bisection.py b/zero-points-with-bisection.py new file mode 100644 index 00000000..ffa55e6c --- /dev/null +++ b/zero-points-with-bisection.py @@ -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)) \ No newline at end of file From 9d91d12de9bd2d19f3edb5bfb6ed49de0a5854b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Mruga=C5=82a?= <66689589+kris007iron@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:20:31 +0000 Subject: [PATCH 3/3] alphabetical sorting --- alphabetical-sorting.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 alphabetical-sorting.py diff --git a/alphabetical-sorting.py b/alphabetical-sorting.py new file mode 100644 index 00000000..a9d6c43c --- /dev/null +++ b/alphabetical-sorting.py @@ -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) \ No newline at end of file