Skip to content

Commit 8c100f7

Browse files
committed
#1 Added Bubble Sort Algo and Documentation
1 parent d2ff15e commit 8c100f7

3 files changed

+31
-0
lines changed

Documentation_bubbleSort.odt

22.4 KB
Binary file not shown.

Documentation_bubbleSort.pdf

159 KB
Binary file not shown.

bubbleSort.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def bubbleSort(arr):
2+
length = len(arr) #finding the length of array to be sorted
3+
4+
for i in range(length): # executing the outer loop
5+
for j in range(0,length-i-1): # execiting the inner loop
6+
if arr[j]>arr[j+1]: # comparing two consecutive values
7+
arr[j], arr[j+1] = arr[j+1], arr[j] #swapping the values if latter is greater
8+
return arr
9+
10+
11+
def bubbleSortOptimized(arr):
12+
length = len(arr) #finding the length of array to be sorted
13+
for i in range(length): # executing the outer loop
14+
for j in range(0,length-i-1): # execiting the inner loop
15+
swapped = False #checking if any swaps take place in the ith interation
16+
if arr[j]>arr[j+1]: # comparing two consecutive values
17+
arr[j], arr[j+1] = arr[j+1], arr[j] #swapping the values if latter is greater
18+
swapped = True
19+
if swapped==False:
20+
break #break off the outer loop if there are no swaps in an iteration
21+
return arr
22+
23+
# arr = [6,87,43,25,7,31,89,90,0,21,43,65,76,23,65,87,23,65,23,31,65,87,433,544,565,876,432]
24+
arr = [3,5,2,4,0,1]
25+
sorted_arr = bubbleSort(arr)
26+
print(sorted_arr)
27+
28+
arr = [3,5,2,4,0,1]
29+
sorted_arr = bubbleSortOptimized(arr)
30+
print(sorted_arr)
31+

0 commit comments

Comments
 (0)