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