From 0ceab85246ac4e47c7e1398fd57dd9b1e4fdaaeb Mon Sep 17 00:00:00 2001 From: Ameet Dhas Date: Sun, 19 May 2019 14:55:12 +0530 Subject: [PATCH] Sort_bubble.py bubble sort --- sorts/Sort_bubble.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 sorts/Sort_bubble.py diff --git a/sorts/Sort_bubble.py b/sorts/Sort_bubble.py new file mode 100644 index 000000000000..8c51a76cc850 --- /dev/null +++ b/sorts/Sort_bubble.py @@ -0,0 +1,23 @@ +def bubbleSort(arr): + n = len(arr) + + # Traverse through all array elements + for i in range(n): + + # Last i elements are already in place + for j in range(0, n-i-1): + + # traverse the array from 0 to n-i-1 + # Swap if the element found is greater + # than the next element + if arr[j] > arr[j+1] : + arr[j], arr[j+1] = arr[j+1], arr[j] + +# Driver code to test above +arr = [64, 34, 25, 12, 22, 11, 90] + +bubbleSort(arr) + +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]),