Skip to content

Commit d286453

Browse files
authored
Merge pull request #302 from siddhi-244/Siddhi/Bubble-sort
Added bubble sort in python.
2 parents 67a7369 + 9b04759 commit d286453

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sorting Algorithms/bubble_sort.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#Bubble sort is a sorting algorithm. Sorting algorithms are used to arrange the array in particular order.In,Bubble sort larger elements are pushed at the end of array in each iteration.It works by repeatedly swapping the adjacent elements if they are in wrong order.
3+
4+
def bubbleSort(a):
5+
n = len(a)
6+
# Traverse through all array elements
7+
8+
for i in range(n-1):
9+
# Last i elements are already in place
10+
for j in range(0, n-i-1):
11+
12+
# traverse the array from 0 to n-i-1
13+
# Swap if the element found is greater
14+
# than the next element
15+
if arr[j] > arr[j + 1] :
16+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
17+
18+
arr = []
19+
n=int(input("Enter size of array: "))
20+
for i in range(n):
21+
e=int(input())
22+
arr.append(e)
23+
bubbleSort(arr)
24+
print ("Sorted array is:")
25+
for i in range(len(arr)):
26+
print(arr[i])
27+
28+
#Time complexity - O(n^2)
29+
#Space complexity - O(1)

0 commit comments

Comments
 (0)