From 86cb5e50ea9ad8b8252444d99b40cb7863a1eaf8 Mon Sep 17 00:00:00 2001 From: chirbard <73120520+chirbard@users.noreply.github.com> Date: Sun, 5 May 2024 12:15:13 +0000 Subject: [PATCH] daily commit 2024-05-05 --- 2024-05-05/README.md | 4 ++++ 2024-05-05/bubble_sort.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 2024-05-05/README.md create mode 100644 2024-05-05/bubble_sort.py diff --git a/2024-05-05/README.md b/2024-05-05/README.md new file mode 100644 index 0000000..858c2c4 --- /dev/null +++ b/2024-05-05/README.md @@ -0,0 +1,4 @@ +# Codecademy bubble sort implementation +This is a bubble sort implementation in Codecademy "Learn Data Structures and Algorithms with Python" course. + +Link to course: https://www.codecademy.com/enrolled/courses/learn-data-structures-and-algorithms-with-python \ No newline at end of file diff --git a/2024-05-05/bubble_sort.py b/2024-05-05/bubble_sort.py new file mode 100644 index 0000000..74b2251 --- /dev/null +++ b/2024-05-05/bubble_sort.py @@ -0,0 +1,20 @@ +nums = [5, 2, 9, 1, 5, 6] + +def swap(arr, index_1, index_2): + temp = arr[index_1] + arr[index_1] = arr[index_2] + arr[index_2] = temp + +# define bubble_sort(): +def bubble_sort(arr): + for el in arr: + for i in range(len(arr) - 1): + if arr[i] > arr[i + 1]: + swap(arr, i, i + 1) + + +##### test statements + +print("Pre-Sort: {0}".format(nums)) +bubble_sort(nums) +print("Post-Sort: {0}".format(nums)) \ No newline at end of file