Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Java.iml
.idea/*
out/
Downloads.iml
22 changes: 22 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# The Algorithms - Java (WORK IN PROGRESS)

## Goal
Make it a working Java project with full fledged test cases for each algorithm and correct package structures. Once we have enough test coverage, we would merge it with master

## Contribution Guidelines
- If you add an algorithm then you have to add a test along with it. In absence of a test the PR would not be approved
- Follow the correct coding guidelines with proper description for the methods. Refer [DecimalToAnyBase.java](https://github.com/TheAlgorithms/Java/blob/Development/src/main/com/java/conversions/DecimalToAnyBase.java) for algorithm and [DecimalToAnyBaseTest.java](https://github.com/TheAlgorithms/Java/blob/Development/src/test/com/java/conversions/DecimalToAnyBaseTest.java) for the test coding standards.
- Do not add a main method as we just need the actual algorithm in a method for the class which we are going to test in the test cases
- Please do not add a signature inside the code. The commit history is sufficient enough to determine who has added the code to the repo.
- Make sure the algorithm which is getting added comes under a certain domain of Algorithms. Please don't create a package with name such as Misc, Others etc.
- While making a PR make sure you are commiting the Java files only and not any project specific files. If you feel that your IDE is generating some extra files then either don't add them to git or add the extensions to ```.gitignore```
- Please don't add solutions to problems from online judge such as Hackerrank, Leetcode etc

## Steps to raise a PR
- Fork to [Java Repo](https://github.com/TheAlgorithms/Java)
- Open the forked repo on your local machine
- Switch to ```Development``` branch by using the command ```git checkout Development```
- Add the JAR for JUnit to your build path. Here is a link for the [JUnit JAR](http://www.java2s.com/Code/Jar/j/Downloadjunit410jar.htm)
- Make the changes on your local machine
- Push the changes to the forked repository
- Raise a PR against the Development branch
Binary file not shown.
Binary file not shown.
Binary file added bin/src/main/java/com/search/BinarySearch.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/BubbleSort.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/HeapSort$Heap.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/HeapSort.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/InsertionSort.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/QuickSort.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/SelectionSort.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/ShellSort.class
Binary file not shown.
Binary file added bin/src/main/java/com/sorts/SortUtils.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added bin/src/test/java/com/sorts/BubbleSortTest.class
Binary file not shown.
Binary file added bin/src/test/java/com/sorts/HeapSortTest.class
Binary file not shown.
Binary file not shown.
Binary file added bin/src/test/java/com/sorts/QuickSortTest.class
Binary file not shown.
Binary file not shown.
Binary file added bin/src/test/java/com/sorts/ShellSortTest.class
Binary file not shown.
27 changes: 27 additions & 0 deletions src/main/java/com/sorts/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package src.main.java.com.sorts;

public class InsertionSort {

/**
* This method implements the Generic Insertion Sort
* Sorts the array in increasing order
*
* @param array The array to be sorted
**/
public <T extends Comparable<T>> T[] sort(T[] array) {
for (int j = 1; j < array.length; j++) {

// Picking up the key(Card)
T key = array[j];
int i = j - 1;

while (i >= 0 && SortUtils.less(key, array[i])) {
array[i + 1] = array[i];
i--;
}
// Placing the key (Card) at its correct position in the sorted subarray
array[i + 1] = key;
}
return array;
}
}
27 changes: 27 additions & 0 deletions src/test/java/com/sorts/InsertionSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package src.test.java.com.sorts;


import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.sorts.InsertionSort;

import java.util.Arrays;

public class InsertionSortTest {

@Test
public void insertionSortTest() {
InsertionSort insertionSort = new InsertionSort();
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(Arrays.toString(insertionSort.sort(unsortedInt)));

Assert.assertArrayEquals(sortedInt, insertionSort.sort(unsortedInt));

Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
System.out.println(Arrays.toString(insertionSort.sort(unsortedChar)));

Assert.assertArrayEquals(sortedChar, insertionSort.sort(unsortedChar));
}
}