diff --git a/python/bubsort.py b/python/bubsort.py new file mode 100644 index 0000000..8f98445 --- /dev/null +++ b/python/bubsort.py @@ -0,0 +1,23 @@ +def bubble_sort(arr): + + # Outer loop to iterate through the list n times + for n in range(len(arr) - 1, 0, -1): + + # Inner loop to compare adjacent elements + for i in range(n): + if arr[i] > arr[i + 1]: + + # Swap elements if they are in the wrong order + swapped = True + arr[i], arr[i + 1] = arr[i + 1], arr[i] + + +# Sample list to be sorted +arr = [39, 12, 18, 85, 72, 10, 2, 18] +print("Unsorted list is:") +print(arr) + +bubble_sort(arr) + +print("Sorted list is:") +print(arr)