Skip to content

Commit f0ac0b1

Browse files
committed
add bubble sort and selection sort implemention with Java
1 parent fd6155a commit f0ac0b1

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

src/java/main/huson/BubbleSort.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package huson;
2+
3+
import java.util.Arrays;
4+
5+
public class BubbleSort implements ISort
6+
{
7+
@Override
8+
public int[] sort(int[] arr)
9+
{
10+
int[] result = Arrays.copyOf(arr, arr.length);
11+
for (int i = 1; i < result.length; i++) {
12+
for (int j = 0; j < result.length - i; j++) {
13+
if (result[j] > result[j + 1]) {
14+
int temp = result[j];
15+
result[j] = result[j + 1];
16+
result[j + 1] = temp;
17+
}
18+
}
19+
20+
}
21+
return result;
22+
}
23+
}

src/java/main/huson/ISort.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package huson;
2+
3+
public interface ISort
4+
{
5+
int[] sort(int[] arr);
6+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package huson;
2+
3+
import java.util.Arrays;
4+
5+
public class SelectionSort implements ISort
6+
{
7+
@Override
8+
public int[] sort(int[] arr)
9+
{
10+
int[] result = Arrays.copyOf(arr, arr.length);
11+
for (int i = 0; i < result.length - 1; i++) {
12+
int minIndex = i;
13+
for (int j = i + 1; j < result.length; j++) {
14+
if (result[minIndex] > result[j]) {
15+
minIndex = j;
16+
}
17+
}
18+
19+
if (minIndex != i) {
20+
int tmp = result[i];
21+
result[i] = result[minIndex];
22+
result[minIndex] = tmp;
23+
}
24+
}
25+
26+
return result;
27+
28+
}
29+
30+
}

src/java/test/HusonSortTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import huson.BubbleSort;
2+
import huson.SelectionSort;
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
import java.util.Random;
10+
11+
public class HusonSortTest
12+
{
13+
private int[] array = null;
14+
private int[] sortedArray = null;
15+
private int number = 200;
16+
private int min = 100;
17+
private int max = 1000;
18+
19+
@Before
20+
public void setArray()
21+
{
22+
array = generateRandomArray(number, min, max);
23+
sortedArray = Arrays.copyOf(array, array.length);
24+
Arrays.sort(sortedArray);
25+
}
26+
27+
@After
28+
public void clearArray()
29+
{
30+
array = null;
31+
sortedArray = null;
32+
}
33+
34+
@Test
35+
public void testBubbleSort()
36+
{
37+
System.out.println("sortedArray = \n" + Arrays.toString(sortedArray));
38+
System.out.println("bubbleSortedArray = \n" + Arrays.toString(new BubbleSort().sort(array)));
39+
Assert.assertArrayEquals(sortedArray, new BubbleSort().sort(array));
40+
}
41+
42+
@Test
43+
public void testSelectionSort()
44+
{
45+
System.out.println("sortedArray = \n" + Arrays.toString(sortedArray));
46+
System.out.println("selectionSortedArray = \n" + Arrays.toString(new SelectionSort().sort(array)));
47+
Assert.assertArrayEquals(sortedArray, new SelectionSort().sort(array));
48+
}
49+
50+
private int[] generateRandomArray(int number, int min, int max)
51+
{
52+
int[] result = new int[number];
53+
54+
for (int i = 0; i < number; i++) {
55+
Random random = new Random();
56+
result[i] = random.nextInt(max - min) + min;
57+
}
58+
return result;
59+
}
60+
}

0 commit comments

Comments
 (0)