diff --git a/src/java/lib/hamcrest-core-1.3.jar b/src/java/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/src/java/lib/hamcrest-core-1.3.jar differ diff --git a/src/java/lib/junit-4.12.jar b/src/java/lib/junit-4.12.jar new file mode 100644 index 0000000..3a7fc26 Binary files /dev/null and b/src/java/lib/junit-4.12.jar differ diff --git a/src/java/main/huson/BubbleSort.java b/src/java/main/huson/BubbleSort.java new file mode 100644 index 0000000..3895063 --- /dev/null +++ b/src/java/main/huson/BubbleSort.java @@ -0,0 +1,23 @@ +package huson; + +import java.util.Arrays; + +public class BubbleSort implements ISort +{ + @Override + public int[] sort(int[] arr) + { + int[] result = Arrays.copyOf(arr, arr.length); + for (int i = 1; i < result.length; i++) { + for (int j = 0; j < result.length - i; j++) { + if (result[j] > result[j + 1]) { + int temp = result[j]; + result[j] = result[j + 1]; + result[j + 1] = temp; + } + } + + } + return result; + } +} diff --git a/src/java/main/huson/ISort.java b/src/java/main/huson/ISort.java new file mode 100644 index 0000000..5df7cb0 --- /dev/null +++ b/src/java/main/huson/ISort.java @@ -0,0 +1,6 @@ +package huson; + +public interface ISort +{ + int[] sort(int[] arr); +} diff --git a/src/java/main/huson/InsertionSort.java b/src/java/main/huson/InsertionSort.java new file mode 100644 index 0000000..c377277 --- /dev/null +++ b/src/java/main/huson/InsertionSort.java @@ -0,0 +1,24 @@ +package huson; + +import java.util.Arrays; + +public class InsertionSort implements ISort +{ + @Override + public int[] sort(int[] arr) + { + int[] result = Arrays.copyOf(arr, arr.length); + for (int i = 1; i < result.length; i++) { + int insertValue = result[i]; + int j = i; + while (j > 0 && insertValue < result[j - 1] ) { + result[j] = result[j - 1]; + j--; + } + if (j != i) { + result[j] = insertValue; + } + } + return result; + } +} \ No newline at end of file diff --git a/src/java/main/huson/SelectionSort.java b/src/java/main/huson/SelectionSort.java new file mode 100644 index 0000000..c35d02e --- /dev/null +++ b/src/java/main/huson/SelectionSort.java @@ -0,0 +1,30 @@ +package huson; + +import java.util.Arrays; + +public class SelectionSort implements ISort +{ + @Override + public int[] sort(int[] arr) + { + int[] result = Arrays.copyOf(arr, arr.length); + for (int i = 0; i < result.length - 1; i++) { + int minIndex = i; + for (int j = i + 1; j < result.length; j++) { + if (result[minIndex] > result[j]) { + minIndex = j; + } + } + + if (minIndex != i) { + int tmp = result[i]; + result[i] = result[minIndex]; + result[minIndex] = tmp; + } + } + + return result; + + } + +} diff --git a/src/java/test/HusonSortTest.java b/src/java/test/HusonSortTest.java new file mode 100644 index 0000000..4c59967 --- /dev/null +++ b/src/java/test/HusonSortTest.java @@ -0,0 +1,69 @@ +import huson.BubbleSort; +import huson.InsertionSort; +import huson.SelectionSort; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Random; + +public class HusonSortTest +{ + private int[] array = null; + private int[] sortedArray = null; + private int number = 200; + private int min = 100; + private int max = 1000; + + @Before + public void setArray() + { + array = generateRandomArray(number, min, max); + sortedArray = Arrays.copyOf(array, array.length); + Arrays.sort(sortedArray); + } + + @After + public void clearArray() + { + array = null; + sortedArray = null; + } + + @Test + public void testBubbleSort() + { + System.out.println("sortedArray = \n" + Arrays.toString(sortedArray)); + System.out.println("bubbleSortedArray = \n" + Arrays.toString(new BubbleSort().sort(array))); + Assert.assertArrayEquals(sortedArray, new BubbleSort().sort(array)); + } + + @Test + public void testSelectionSort() + { + System.out.println("sortedArray = \n" + Arrays.toString(sortedArray)); + System.out.println("selectionSortedArray = \n" + Arrays.toString(new SelectionSort().sort(array))); + Assert.assertArrayEquals(sortedArray, new SelectionSort().sort(array)); + } + + @Test + public void testInsertionSort() + { + System.out.println("sortedArray = \n" + Arrays.toString(sortedArray)); + System.out.println("selectionSortedArray = \n" + Arrays.toString(new InsertionSort().sort(array))); + Assert.assertArrayEquals(sortedArray, new InsertionSort().sort(array)); + } + + private int[] generateRandomArray(int number, int min, int max) + { + int[] result = new int[number]; + + for (int i = 0; i < number; i++) { + Random random = new Random(); + result[i] = random.nextInt(max - min) + min; + } + return result; + } +}