|
1 | 1 | """
|
2 |
| -Bubble Sort implementation in python 3.0 |
| 2 | +Bubble Sort Algorithm implementation in python 3.0 |
| 3 | +
|
| 4 | +I have implemented three functions in this file, each implementing bubble sort but each with |
| 5 | +some extra technique which can drastically affect runtime, as the size of array increases. |
| 6 | +
|
| 7 | +To compare the running time of each, I have used a variable num_of_comparisions. |
3 | 8 | """
|
4 | 9 |
|
| 10 | +def bubble_sort(arr): |
| 11 | + """ Naive approach of bubble sort """ |
| 12 | + num_of_comparisions = 0 |
| 13 | + for i in range(len(arr)-1): |
| 14 | + # number of comparisions in each iteration is len(array) - 1 |
| 15 | + for j in range(len(arr)-1): |
| 16 | + num_of_comparisions += 1 |
| 17 | + # compare the two elements |
| 18 | + # if the first element is greater than the one after it, swap them |
| 19 | + # after the all the comparisions, the biggest element ends up at the last position of the array |
| 20 | + if arr[j] > arr[j+1]: |
| 21 | + temp = arr[j+1] |
| 22 | + arr[j+1] = arr[j] |
| 23 | + arr[j] = temp |
| 24 | + print(num_of_comparisions) |
| 25 | + return arr |
| 26 | + |
| 27 | + |
| 28 | +def bubble_sort_intermediate(arr): |
| 29 | + """ The difference here is that, I am stopping the iterations once the array is sorted. """ |
| 30 | + num_of_comparisions = 0 |
| 31 | + for i in range(len(arr)-1): |
| 32 | + count = 0 |
| 33 | + for j in range(len(arr)-1): |
| 34 | + num_of_comparisions += 1 |
| 35 | + if arr[j] > arr[j+1]: |
| 36 | + temp = arr[j+1] |
| 37 | + arr[j+1] = arr[j] |
| 38 | + arr[j] = temp |
| 39 | + count += 1 |
| 40 | + # Checks, if the array is sorted |
| 41 | + if count == 0: |
| 42 | + break |
| 43 | + print(num_of_comparisions) |
| 44 | + return arr |
| 45 | + |
| 46 | + |
| 47 | +def bubble_sort_advanced(arr): |
| 48 | + """ The difference here is at each iteration, I am reducing the length variable, |
| 49 | + which stops the extra number of steps that the code was taking by comparing the elements |
| 50 | + that were already sorted. """ |
| 51 | + num_of_comparisions = 0 |
| 52 | + length = len(arr) - 1 |
| 53 | + for i in range(len(arr)-1): |
| 54 | + count = 0 |
| 55 | + for j in range(length): |
| 56 | + num_of_comparisions += 1 |
| 57 | + if arr[j] > arr[j+1]: |
| 58 | + temp = arr[j+1] |
| 59 | + arr[j+1] = arr[j] |
| 60 | + arr[j] = temp |
| 61 | + count += 1 |
| 62 | + length -= 1 |
| 63 | + if count == 0: |
| 64 | + break |
| 65 | + print(num_of_comparisions) |
| 66 | + return arr |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + # test case |
| 71 | + a = [1,24,35,45,34,4,89,77,7,87,76,7,56,53,44,6,4,43,54,54,6,8,78,23,4,7,78,77,4,42,34,23,23] |
| 72 | + |
| 73 | + # output - |
| 74 | + # num_of_comparisions = 1024 |
| 75 | + # [1, 4, 4, 4, 4, 6, 6, 7, 7, 7, 8, 23, 23, 23, 24, 34, 34, 35, 42, 43, 44, 45, 53, 54, 54, 56, 76, 77, 77, 78, 78, 87, 89] |
| 76 | + print(bubble_sort(a)) |
| 77 | + |
| 78 | + # output - |
| 79 | + # num_of_comparisions = 800 |
| 80 | + # [1, 4, 4, 4, 4, 6, 6, 7, 7, 7, 8, 23, 23, 23, 24, 34, 34, 35, 42, 43, 44, 45, 53, 54, 54, 56, 76, 77, 77, 78, 78, 87, 89] |
| 81 | + a = [1,24,35,45,34,4,89,77,7,87,76,7,56,53,44,6,4,43,54,54,6,8,78,23,4,7,78,77,4,42,34,23,23] |
| 82 | + print(bubble_sort_intermediate(a)) |
| 83 | + |
| 84 | + # output - |
| 85 | + # num_of_comparisions = 500 |
| 86 | + # [1, 4, 4, 4, 4, 6, 6, 7, 7, 7, 8, 23, 23, 23, 24, 34, 34, 35, 42, 43, 44, 45, 53, 54, 54, 56, 76, 77, 77, 78, 78, 87, 89] |
| 87 | + a = [1,24,35,45,34,4,89,77,7,87,76,7,56,53,44,6,4,43,54,54,6,8,78,23,4,7,78,77,4,42,34,23,23] |
| 88 | + print(bubble_sort_advanced(a)) |
0 commit comments