Skip to content

Commit af61003

Browse files
committed
Completed Sorting Algorithms (Insertion, Merge,Radix)
1 parent dee6005 commit af61003

7 files changed

+311
-0
lines changed

Diff for: Git Screenshots/.DS_Store

6 KB
Binary file not shown.

Diff for: SortingAlgorithm.iml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/Tests" isTestSource="true" />
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
</content>
9+
<orderEntry type="inheritedJdk" />
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="module-library" scope="TEST">
12+
<library name="JUnit4">
13+
<CLASSES>
14+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
15+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
16+
</CLASSES>
17+
<JAVADOC />
18+
<SOURCES />
19+
</library>
20+
</orderEntry>
21+
<orderEntry type="module-library">
22+
<library name="JUnit5.8.1">
23+
<CLASSES>
24+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
25+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
26+
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
27+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
28+
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
29+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
30+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
31+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
32+
</CLASSES>
33+
<JAVADOC />
34+
<SOURCES />
35+
</library>
36+
</orderEntry>
37+
<orderEntry type="module-library">
38+
<library name="JUnit5.8.1">
39+
<CLASSES>
40+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
41+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
42+
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
43+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
44+
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
45+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
46+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
47+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
48+
</CLASSES>
49+
<JAVADOC />
50+
<SOURCES />
51+
</library>
52+
</orderEntry>
53+
<orderEntry type="module-library">
54+
<library name="testng">
55+
<CLASSES>
56+
<root url="jar://$MAVEN_REPOSITORY$/org/testng/testng/7.1.0/testng-7.1.0.jar!/" />
57+
<root url="jar://$MAVEN_REPOSITORY$/com/beust/jcommander/1.72/jcommander-1.72.jar!/" />
58+
<root url="jar://$MAVEN_REPOSITORY$/com/google/inject/guice/4.1.0/guice-4.1.0-no_aop.jar!/" />
59+
<root url="jar://$MAVEN_REPOSITORY$/javax/inject/javax.inject/1/javax.inject-1.jar!/" />
60+
<root url="jar://$MAVEN_REPOSITORY$/aopalliance/aopalliance/1.0/aopalliance-1.0.jar!/" />
61+
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0.jar!/" />
62+
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.21/snakeyaml-1.21.jar!/" />
63+
</CLASSES>
64+
<JAVADOC />
65+
<SOURCES />
66+
</library>
67+
</orderEntry>
68+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
69+
</component>
70+
</module>

Diff for: src/InsertionSort.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class InsertionSort implements SortingAlgoInterface {
2+
/**
3+
* Sorts the array using the insertion sort algorithm
4+
* @param array array to be sorted
5+
*/
6+
public void sort(int[]array) {
7+
int current;
8+
int j;
9+
for(int i = 1; i < array.length; i++) {
10+
current = array[i];
11+
for(j = i; j > 0 && current < array[j-1]; j--) {
12+
array[j] = array[j - 1];
13+
}
14+
array[j] = current;
15+
}
16+
}
17+
/**
18+
* Gets the name of the sorting algorithm
19+
* @return name of the sorting algorithm
20+
*/
21+
@Override
22+
public String getName() {
23+
return "InsertSort";
24+
}
25+
}

Diff for: src/JUnitTest.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import org.junit.jupiter.api.Test;
2+
import java.util.Random;
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
public class JUnitTest {
6+
public static boolean contains(int[] data, int target) {
7+
for (int datum : data) {
8+
if (datum == target) {
9+
return true;
10+
}
11+
}
12+
return false;
13+
}
14+
static int[] makeList() {
15+
Random random = new Random();
16+
int bound = 1000; //upper bound of values
17+
int[] list = new int[bound / 2];
18+
int num;
19+
for (int i = 0; i < list.length; i++) {
20+
num = random.nextInt(bound);
21+
if (!(contains(list, num))) {
22+
list[i] = num;
23+
} else {
24+
i--;
25+
}
26+
}
27+
return list;
28+
}
29+
@Test
30+
void testAnySort(SortingAlgoInterface obj) {
31+
32+
int[] list = makeList();
33+
34+
System.out.println();
35+
System.out.println(" \t \t" + obj.getName() + " \t \t");
36+
System.out.println("==============================================");
37+
//Prints the first 10 of unsorted list to console
38+
for (int i = 0; i < 10; i++) {
39+
System.out.print(list[i] + " ");
40+
}
41+
System.out.println();
42+
obj.sort(list);
43+
for (int i = 1; i < list.length; i++) {
44+
assertTrue(list[i - 1] < list[i]);
45+
}
46+
// Prints the first 15 values to console
47+
for (int i = 0; i < 15; i++) {
48+
System.out.print(list[i] + " ");
49+
}
50+
System.out.println();
51+
System.out.println("==============================================");
52+
}
53+
@Test
54+
void testInsertionSort() {
55+
InsertionSort obj = new InsertionSort();
56+
testAnySort(obj);
57+
}
58+
@Test
59+
void testMergeSort() {
60+
MergeSort obj = new MergeSort();
61+
testAnySort(obj);
62+
}
63+
@Test
64+
void testRadixSort() {
65+
RadixSort obj = new RadixSort();
66+
testAnySort(obj);
67+
}
68+
}

Diff for: src/MergeSort.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
public class MergeSort implements SortingAlgoInterface {
2+
/**
3+
* Merges two sorted arrays into a single sorted arrays
4+
* @param array array to be merged
5+
* @param left left sub-array
6+
* @param right right sub-array
7+
*/
8+
private static void merge(int[] array, int[] left, int[] right) {
9+
int index = 0;
10+
int leftIndex = 0;
11+
int rightIndex = 0;
12+
while(leftIndex < left.length && rightIndex < right.length) {
13+
if (left[leftIndex] < right[rightIndex]) {
14+
array[index++] = left[leftIndex++];
15+
} else {
16+
array[index++] = right[rightIndex++];
17+
}
18+
}
19+
while(leftIndex < left.length){
20+
array[index++] = left[leftIndex++];
21+
}
22+
while(rightIndex < right.length){
23+
array[index++] = right[rightIndex++];
24+
}
25+
}
26+
/**
27+
* Retrieves the first half of the array
28+
* @param array input array
29+
* @return first half of the array
30+
*/
31+
private static int[] getFirstHalf(int [] array){
32+
int[] result = new int[array.length/2];
33+
for(int i = 0; i < result.length;i++) {
34+
result[i] = array[i];
35+
}
36+
return result;
37+
}
38+
/**
39+
* Retrieves the second half of the array
40+
* @param array input array
41+
* @return second half of the array
42+
*/
43+
private static int[] getSecondHalf(int [] array) {
44+
int[] result = new int[array.length/2 + array.length % 2];
45+
int start = array.length / 2;
46+
for(int i = 0; i < result.length;i++) {
47+
result[i] = array[i + start];
48+
}
49+
return result;
50+
}
51+
/**
52+
* Main sorting function that recursively sorts
53+
* the array using merge sort
54+
* @param array array to be sorted
55+
*/
56+
@Override
57+
public void sort(int[] array) {
58+
if (array.length > 1) {
59+
int[] left = getFirstHalf(array);
60+
int[] right = getSecondHalf(array);
61+
sort(left);
62+
sort(right);
63+
merge(array, left, right);
64+
}
65+
}
66+
/**
67+
* Get the name of the sorting algorithm
68+
* @return name of the sorting algorithm
69+
*/
70+
@Override
71+
public String getName() {
72+
return "MergeSort";
73+
}
74+
75+
}

Diff for: src/RadixSort.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.Arrays;
2+
3+
public class RadixSort implements SortingAlgoInterface{
4+
/**
5+
* A function to get the maximum value in the array
6+
* @param array input array
7+
* @param n size of the array
8+
* @return maximum value in the array
9+
*/
10+
static int getMax(int array[], int n) {
11+
int mx = array[0];
12+
for (int i = 1; i < n; i++) {
13+
if (array[i] > mx)
14+
mx = array[i];
15+
}
16+
return mx;
17+
}
18+
/**
19+
* A function to perform counting sort of the array
20+
* base to the digit with the exp
21+
* @param array input array
22+
* @param n size of the array
23+
* @param exp current digit position
24+
*/
25+
static void countSort(int[]array, int n, int exp) {
26+
int[]output = new int[n];
27+
int i;
28+
int[] count = new int[10];
29+
Arrays.fill(count, 0);
30+
31+
for (i = 0; i < n; i++) {
32+
count[(array[i] / exp) % 10]++;
33+
}
34+
for (i = 1; i < 10; i++) {
35+
count[i] += count[i - 1];
36+
}
37+
for (i = n - 1; i >= 0; i--) {
38+
output[count[(array[i] / exp) % 10] - 1] = array[i];
39+
count[(array[i] / exp) % 10]--;
40+
}
41+
for (i = 0; i < n; i++) {
42+
array[i] = output[i];
43+
}
44+
}
45+
46+
/**
47+
* The main sorting function that sorts the array
48+
* using Radix Sort
49+
* @param array array to be sorted
50+
*/
51+
@Override
52+
public void sort(int array[]) {
53+
int n = array.length;
54+
int m = getMax(array, n);
55+
for (int exp = 1; m / exp > 0; exp *= 10) {
56+
countSort(array, n, exp);
57+
}
58+
}
59+
60+
/**
61+
* Gets the name of the sorting algorithm
62+
* @return name of the sorting algorithm
63+
*/
64+
@Override
65+
public String getName() {
66+
return "RadixSort";
67+
}
68+
}

Diff for: src/SortingAlgoInterface.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public interface SortingAlgoInterface {
2+
public void sort(int[] array);
3+
4+
public String getName();
5+
}

0 commit comments

Comments
 (0)