Skip to content

Commit 58d64b2

Browse files
jayeshcpblakeembrey
authored andcommitted
BubbleSort: Fix to reduce number of iterations for sorted array as input (blakeembrey#168)
Previous solution performed `O(N ^ 2)` iterations for sorted input. This fixes it by exiting loop if array is already sorted and performs at most `O(N)` iterations in this case.
1 parent 8889303 commit 58d64b2

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

solutions/java/BubbleSort.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
* compares each pair of adjacent items and swaps them if they are in the wrong order.
44
* The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
55
* Author : Viveka Aggarwal
6+
* Author : Jayesh Chandrapal
67
*/
78

8-
public class bubbleSort {
9+
public class BubbleSort {
910
public static void sort(int[] arr) {
10-
int n = arr.length-2;
11-
while(n > 0) {
12-
for(int j = 0 ; j <= n ; j++) {
13-
if(arr[j] >= arr[j+1])
14-
swap(j, j+1, arr);
11+
int len = arr.length;
12+
boolean unsorted = true;
13+
14+
while(unsorted) {
15+
unsorted = false;
16+
17+
for(int j = 0 ; j < len - 1; j++) {
18+
if(arr[j] > arr[j + 1]) {
19+
swap(j, j + 1, arr);
20+
unsorted = true;
21+
}
1522
}
16-
n--;
1723
}
1824
}
1925

@@ -24,8 +30,11 @@ public static void swap(int a, int b, int[] arr) {
2430
}
2531

2632
public static void main(String[] a) {
27-
int[] arr = {23, 3, 12, 54, 34, 77, 78, 87, 92, 12};
33+
int[] arr = {23, 3, 12, 54, 34, 77, 78, 87, 92, 12}; // unsorted input
34+
//int[] arr = {3,12, 12, 23, 34, 54, 77, 78, 87, 92}; // sorted input
35+
2836
sort(arr);
37+
2938
for(int i : arr) {
3039
System.out.print(i + " ");
3140
}

solutions/python/bubble-sort.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1+
import unittest
12

23
def bubblesort(lst):
3-
for i in range(0,len(lst)-1):
4-
for j in range(i+1,len(lst)):
5-
if lst[i]>lst[j]:
6-
temp=lst[i]
7-
lst[i]=lst[j]
8-
lst[j]=temp
4+
for i in range(0, len(lst) - 1):
5+
for j in range(i + 1, len(lst)):
6+
if lst[i] > lst[j]:
7+
# swap values
8+
lst[i], lst[j] = lst[j], lst[i]
99
return lst
10-
11-
l=[1,4,2,4,6,2,6,8,5,8,9,5]
12-
for i in bubblesort(l):
13-
print (i)
10+
11+
class Test(unittest.TestCase):
12+
def testUnsortedSmall(self):
13+
self.assertEqual([1,2,3,4], bubblesort([4,3,2,1]))
14+
15+
def testSortedSmall(self):
16+
self.assertEqual([1,2,3,4], bubblesort([1,2,3,4]))
17+
18+
def testUnsortedLarge(self):
19+
self.assertEqual([1,2,3,4,5,6,7,8,9,10], bubblesort([6,3,4,8,7,9,5,10,1,2]))
20+
21+
def testSortedLarge(self):
22+
self.assertEqual([1,2,3,4,5,6,7,8,9,10], bubblesort([1,2,3,4,5,6,7,8,9,10]))
23+
24+
def testSingleInput(self):
25+
self.assertEqual([1], bubblesort([1]))
26+
27+
def testBlankInput(self):
28+
self.assertEqual([], bubblesort([]))
29+
30+
if __name__ == '__main__':
31+
unittest.main()

0 commit comments

Comments
 (0)