From 1d99bd1bb52bbc5351303ef1fd7c807d65fc541c Mon Sep 17 00:00:00 2001 From: Saloni Date: Wed, 17 Mar 2021 10:10:59 +0530 Subject: [PATCH] Fixed minor bugs and added sample I/O --- Python/sort/Bubble_Sort.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Python/sort/Bubble_Sort.py b/Python/sort/Bubble_Sort.py index 7be67a329a..300fde4b76 100644 --- a/Python/sort/Bubble_Sort.py +++ b/Python/sort/Bubble_Sort.py @@ -5,24 +5,40 @@ the most heaviest element shifts to the last position. The flag is used to check if any swapping is performed or not. If no swapping is done, the loop breaks as the array is already sorted. -Time Complexity: O(n^2) -Space Complexity: O(1) Return Type: No return type as lists are mutable ''' def bubble_sort(Array): - for i in range (len(Array)-1): + + n = len(Array) + for i in range(n-1): flag = True - for j in range (len(Array)-1): - if (Array[j] > Array[j+1]): + for j in range(n-i-1): + if Array[j] > Array[j+1]: flag = False Array[j], Array[j+1] = Array[j+1], Array[j] if flag: break -if __name__=="__main__": + +if __name__ == "__main__": + print("Enter the elements of the list to be sorted: ") Array = [int(x) for x in input().split(" ")] bubble_sort(Array) print("List after sorting: ") for i in Array: - print(i,end=" ") + print(i, end=" ") + + +''' +Sample Input: +Enter the elements of the list to be sorted: +2 5 1 6 3 9 10 8 + +Sample Output: +List after sorting: +1 2 3 5 6 8 9 10 + +Time Complexity: O(n^2) +Space Complexity: O(1) +'''