Skip to content

Commit 8aed601

Browse files
committed
Bubble sort added.
1 parent c92ce4c commit 8aed601

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

BubbleSort/Bubble.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package BubbleSort;
2+
3+
/**
4+
* Bubble sort is a sorting algorithm that is implemented by starting in the
5+
* beginning of the array and swapping the first two elements only if the first
6+
* element is greater than the second element. This comparison is then moved
7+
* onto the next pair and so on and so forth. This is done until the array is
8+
* completely sorted. The smaller items slowly “bubble” up to the beginning of
9+
* the array.
10+
*
11+
* @author Guillermo Facundo Colunga.
12+
* @version 1
13+
* @since 201712120028.
14+
* @formatter Oviedo Computing Community Extended.
15+
* @param <T> is the type to sort.
16+
*/
17+
public class Bubble<T extends Comparable<T>> {
18+
19+
/**
20+
* Average: O(N^2) Worst: O(N^2) Memory: O(1)
21+
*
22+
* Sorts the given array of elements by executing bubble sort. That is by
23+
* starting in the beginning of the array and swapping the first two
24+
* elements only if the first element is greater than the second element.
25+
* This comparison is then moved onto the next pair and so on and so forth.
26+
* This is done until the array is completely sorted. The smaller items
27+
* slowly “bubble” up to the beginning of the array.
28+
*
29+
* @param elements array to be sorted. It is sorted in the same array.
30+
*/
31+
public void sort( T[] elements ) {
32+
for (int i = 1; i < elements.length; i++) { // O(n)
33+
for (int j = elements.length - 1; j >= i; j--) { // O(n)
34+
if (elements[j - 1].compareTo( elements[j] ) > 0) {
35+
T temp = elements[j];
36+
elements[j] = elements[j - 1];
37+
elements[j - 1] = temp;
38+
}
39+
}
40+
}
41+
}
42+
43+
}

BubbleSort/tests/BubbleTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package BubbleSort.tests;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
import BubbleSort.Bubble;
8+
9+
public class BubbleTest {
10+
11+
private Integer[] intArr = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
12+
sortIntArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13+
14+
private String[] strArr = { "d", "c", "b", "a" },
15+
sortStrArr = { "a", "b", "c", "d" };
16+
17+
private Double[] dblArr = { 1.9, 2.6, Double.MAX_VALUE, 0.00004, Double.MIN_VALUE },
18+
sortDblArr = { Double.MIN_VALUE, 0.00004, 1.9, 2.6, Double.MAX_VALUE };
19+
20+
@Test public void integerSortTest() {
21+
new Bubble<Integer>().sort( intArr );
22+
assertArrayEquals( sortIntArr, intArr );
23+
}
24+
25+
@Test public void stringSortTest() {
26+
new Bubble<String>().sort( strArr );
27+
assertArrayEquals( sortStrArr, strArr );
28+
}
29+
30+
@Test public void doubleSortTest() {
31+
new Bubble<Double>().sort( dblArr );
32+
assertArrayEquals( sortDblArr, dblArr );
33+
}
34+
35+
}

0 commit comments

Comments
 (0)