## Merge Sort Algorithm using Generics in Java

Today, we are going to show the implementation of the Merge Sort algorithm, which is the fifth one from our series of tutorials on sorting algorithms. If you haven’t read the first four tutorials on BubbleSortInsertionSortSelectionSort and CountingSort, I strongly recommend that you go there and read them, because we will reuse code that was explained there.

The application is really simple, it will just get an instance of a Sortable algorithm, which in this case will be a MergeSort object and pass some Integer and String arrays to it, but any object that implements Comparable can be passed as a parameter as well, then it will print the elements from the sorted arrays.

```public class SorterApp {
public static void main(String[] args) {
SorterApp app = new SorterApp();
app.run();
}

private void run() {
sort(new Integer[] { 2, 42, -3, 2, 4 });
sort(new Integer[] { 1, 2, 3, 4, 5 });
sort(new Integer[] { 3, 1, 5, 4, 2 });
sort(new Integer[] { 5, 4, 3, 2, 1 });

System.out.println();

sort(new String[] { "a", "b", "c", "d", "e" });
sort(new String[] { "c", "a", "e", "d", "b" });
sort(new String[] { "e", "d", "c", "b", "a" });
}

private &amp;lt;T&amp;gt; extends Comparable&amp;lt;T&amp;gt; void sort(T[] values) {
Sortable sorter = newSortable(5);
sorter.sortAscending(values);
// sorter.sortDescending(values);
Utils.printArray(values);
}

private Sortable newSortable(int sortableAlgorithm) {
switch (sortableAlgorithm) {
case 1:
return new BubbleSort();
case 2:
return new InsertionSort();
case 3:
return new SelectionSort();
case 4:
return new CountingSort();
case 5:
return new MergeSort();
default:
return new BubbleSort();
}
}
}
```

Today, we are going to show the implementation of the Merge Sort algorithm, which is the fifth one from our series of tutorials on sorting algorithms. If you haven’t read the first four tutorials on BubbleSortInsertionSortSelectionSort and CountingSort, I strongly recommend that you go there and read them, because we will reuse code that was explained there.

The application is really simple, it will just get an instance of a Sortable algorithm, which in this case will be a MergeSort object and pass some Integer and String arrays to it, but any object that implements Comparable can be passed as a parameter as well, then it will print the elements from the sorted arrays.

```public class SorterApp {
public static void main(String[] args) {
SorterApp app = new SorterApp();
app.run();
}

private void run() {
sort(new Integer[] { 2, 42, -3, 2, 4 });
sort(new Integer[] { 1, 2, 3, 4, 5 });
sort(new Integer[] { 3, 1, 5, 4, 2 });
sort(new Integer[] { 5, 4, 3, 2, 1 });

System.out.println();

sort(new String[] { "a", "b", "c", "d", "e" });
sort(new String[] { "c", "a", "e", "d", "b" });
sort(new String[] { "e", "d", "c", "b", "a" });
}

private &amp;lt;T&amp;gt; extends Comparable&amp;lt;T&amp;gt; void sort(T[] values) {
Sortable sorter = newSortable(5);
sorter.sortAscending(values);
// sorter.sortDescending(values);
Utils.printArray(values);
}

private Sortable newSortable(int sortableAlgorithm) {
switch (sortableAlgorithm) {
case 1:
return new BubbleSort();
case 2:
return new InsertionSort();
case 3:
return new SelectionSort();
case 4:
return new CountingSort();
case 5:
return new MergeSort();
default:
return new BubbleSort();
}
}
}
```

## Counting Sort Algorithm in Java

Today, we are going to show the implementation of the Counting sort algorithm, which is the forth one from our series of tutorials on sorting algorithms. If you haven’t read the first three tutorials on BubbleSortInsertionSort and SelectionSort, I strongly recommend that you read them, because we will reuse code that was explained there.

The application is really simple, it will just get an instance of a Sortable algorithm, which in this case will be a CountingSort object and pass some Integer and String arrays to it, but unlike the other three algorithms that are able to sort any object that implements Comparable, this algorithm only works with Integers (positive and negative values), the arrays of String will be ignored. The last part of the code is to print the sorted arrays.

```public class SorterApp {
public static void main(String[] args) {
SorterApp app = new SorterApp();
app.run();
}

private void run() {
sort(new Integer[] { 2, 42, -3, 2, 4 });
sort(new Integer[] { 1, 2, 3, 4, 5 });
sort(new Integer[] { 3, 1, 5, 4, 2 });
sort(new Integer[] { 5, 4, 3, 2, 1 });

System.out.println();

sort(new String[] { "a", "b", "c", "d", "e" });
sort(new String[] { "c", "a", "e", "d", "b" });
sort(new String[] { "e", "d", "c", "b", "a" });
}

private &amp;amp;amp;lt;T extends Comparable&amp;amp;amp;lt;T&amp;amp;amp;gt;&amp;amp;amp;gt; void sort(T[] values) {
Sortable sorter = newSortable(4);
sorter.sortAscending(values);
// sorter.sortDescending(values);
Utils.printArray(values);
}

private Sortable newSortable(int sortableAlgorithm) {
switch (sortableAlgorithm) {
case 1:
return new BubbleSort();
case 2:
return new InsertionSort();
case 3:
return new SelectionSort();
case 4:
return new CountingSort();
default:
return new BubbleSort();
}
}
}

Continue reading Counting Sort Algorithm in Java
```

## Selection Sort Algorithm using Generics in Java

Today, we are going to show the implementation of the Selection Sort algorithm, which is the third one from our series of tutorials on sorting algorithms. If you haven’t read the first two tutorials on BubbleSort and InsertionSort, I strongly recommend that you go there and read them, because we will reuse code that was explained there.

The application is really simple, it will just get an instance of a Sortable algorithm, which in this case will be an SelectionSort object and pass some Integer and String arrays to it, but any object that implements Comparable can be passed as a parameter as well, then it will print the elements from the arrays.

```public class SorterApp {
public static void main(String[] args) {
SorterApp app = new SorterApp();
app.run();
}

private void run() {
sort(new Integer[] { 1, 2, 3, 4, 5 });
sort(new Integer[] { 3, 1, 5, 4, 2 });
sort(new Integer[] { 5, 4, 3, 2, 1 });

System.out.println();

sort(new String[] { "a", "b", "c", "d", "e" });
sort(new String[] { "c", "a", "e", "d", "b" });
sort(new String[] { "e", "d", "c", "b", "a" });
}

private <T> extends Comparable<T> void sort(T[] values) {
Sortable sorter = newSortable(3);
sorter.sortAscending(values);
// sorter.sortDescending(values);
Utils.printArray(values);
}

private Sortable newSortable(int sortableAlgorithm) {
switch (sortableAlgorithm) {
case 1:
return new BubbleSort();
case 2:
return new InsertionSort();
case 3:
return new SelectionSort();
default:
return new BubbleSort();
}
}
}
```

You can see on the code below that the sortAscending and sortDescending invoke the same private method sort with the difference of just 2 parameters. This is done because the code to sort in ascending or descending order is pretty much the same; the difference is just one small detail.

Continue reading Selection Sort Algorithm using Generics in Java

## Insertion Sort Algorithm using Generics in Java

Today, we are going to show the implementation of the Insertion Sort, which is the second algorithm of our series of tutorials on sorting algorithms. If you haven’t read the first tutorial on BubbleSort, I strongly recommend that you go there and read it, because we will reuse code that was explained there.

The application is really simple, it will just get an instance of a Sortable algorithm, which in this case will be an InsertionSort object and pass some Integer and String arrays to it, but any object that implements Comparable can be passed as a parameter as well, then it will print the elements from the arrays.

```public class SorterApp {
public static void main(String[] args) {
SorterApp app = new SorterApp();
app.run();
}

private void run() {
sort(new Integer[] { 1, 2, 3, 4, 5 });
sort(new Integer[] { 3, 1, 5, 4, 2 });
sort(new Integer[] { 5, 4, 3, 2, 1 });

System.out.println();

sort(new String[] { "a", "b", "c", "d", "e" });
sort(new String[] { "c", "a", "e", "d", "b" });
sort(new String[] { "e", "d", "c", "b", "a" });
}

private &amp;amp;amp;amp;amp;amp;amp;lt;T extends Comparable&amp;amp;amp;amp;amp;amp;amp;lt;T&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;gt; void sort(T[] values) {
Sortable sorter = newSortable(2);
sorter.sortAscending(values);
// sorter.sortDescending(values);
Utils.printArray(values);
}

private Sortable newSortable(int sortableAlgorithm) {
switch (sortableAlgorithm) {
case 1:
return new BubbleSort();
case 2:
return new InsertionSort();
default:
return new BubbleSort();
}
}
}

Continue reading Insertion Sort Algorithm using Generics in Java
```

## Bubble Sort Algorithm using Generics in Java

This is our first tutorial about sorting algorithms and we are going to start by showing the Bubble Sort. It gets its name from the way smaller elements “bubble” to the top of the list.

We will create other sorting algorithms, so the first thing we need to do is prepare the structure of classes and packages that we are going to use.

Every sorting algorithm that we are going to create will have at least 2 public methods, which are: sortAscending and sortDescending. To make sure this will always be true, we are going to create an Interface called Sortable.

```public interface Sortable {
<T> extends Comparable<T> void sortAscending(T[] values);
<T> extends Comparable<T> void sortDescending(T[] values);
}
```
Continue reading Bubble Sort Algorithm using Generics in Java