Updated Readme and Implemented Merge Sort
This commit is contained in:
parent
a111922827
commit
aea02838e3
107
MergeSort.java
Normal file
107
MergeSort.java
Normal file
@ -0,0 +1,107 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Merge Sort
|
||||
*
|
||||
*/
|
||||
public class MergeSort {
|
||||
private int[] array;
|
||||
private int[] tempMergArr;
|
||||
private int length;
|
||||
|
||||
/**
|
||||
* Sorts {@code inputArr} with merge sort algorithm.
|
||||
*
|
||||
* @param inputArr
|
||||
*/
|
||||
public final void sort(int inputArr[]) {
|
||||
this.array = inputArr;
|
||||
this.length = inputArr.length;
|
||||
this.tempMergArr = new int[this.length];
|
||||
this.mergeSort(0, this.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions Array into recursively smaller pieces.
|
||||
*
|
||||
* @param lowerIndex
|
||||
* lower bound to include in the first partition
|
||||
* @param higherIndex
|
||||
* upper bound to include in the third partition
|
||||
*/
|
||||
private void mergeSort(int lowerIndex, int higherIndex) {
|
||||
if (lowerIndex < higherIndex) {
|
||||
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
|
||||
// Below step sorts the left side of the array
|
||||
this.mergeSort(lowerIndex, middle);
|
||||
// Below step sorts the right side of the array
|
||||
this.mergeSort(middle + 1, higherIndex);
|
||||
// Now merge both sides
|
||||
this.mergeParts(lowerIndex, middle, higherIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges partitions.
|
||||
*
|
||||
* @param lowerIndex
|
||||
* @param middle
|
||||
* @param higherIndex
|
||||
*/
|
||||
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
|
||||
for (int i = lowerIndex; i <= higherIndex; i++) {
|
||||
this.tempMergArr[i] = this.array[i];
|
||||
}
|
||||
int i = lowerIndex;
|
||||
int j = middle + 1;
|
||||
int k = lowerIndex;
|
||||
while (i <= middle && j <= higherIndex) {
|
||||
if (this.tempMergArr[i] <= this.tempMergArr[j]) {
|
||||
this.array[k] = this.tempMergArr[i];
|
||||
i++;
|
||||
} else {
|
||||
this.array[k] = this.tempMergArr[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while (i <= middle) {
|
||||
this.array[k] = this.tempMergArr[i];
|
||||
k++;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets input to sort.
|
||||
*
|
||||
* @return unsorted array of integers to sort
|
||||
*/
|
||||
public static int[] getInput() {
|
||||
final int numElements = 6;
|
||||
int[] unsorted = new int[numElements];
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for (int i = 0; i < numElements; i++) {
|
||||
unsorted[i] = input.nextInt();
|
||||
}
|
||||
input.close();
|
||||
return unsorted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Method.
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
int[] inputArr = getInput();
|
||||
MergeSort mergeSort = new MergeSort();
|
||||
mergeSort.sort(inputArr);
|
||||
for (int i : inputArr) {
|
||||
System.out.print(i);
|
||||
System.out.print(" ");
|
||||
}
|
||||
}
|
||||
}
|
99
README.md
99
README.md
@ -1,2 +1,97 @@
|
||||
# Java
|
||||
All Algorithms implemented in Java
|
||||
# The Algorithms - Java [![Build Status](https://travis-ci.org/TheAlgorithms/Python.svg)](https://travis-ci.org/TheAlgorithms/Python)
|
||||
|
||||
### All algorithms implemented in Java (for education)
|
||||
|
||||
These are for demonstration purposes only. There are many implementations of sorts in the Java standard library that are much better for performance reasons.
|
||||
|
||||
## Sort Algorithms
|
||||
|
||||
|
||||
### Bubble
|
||||
![alt text][bubble-image]
|
||||
|
||||
From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n^2)
|
||||
|
||||
###### View the algorithm in [action][bubble-toptal]
|
||||
|
||||
|
||||
|
||||
### Insertion
|
||||
![alt text][insertion-image]
|
||||
|
||||
From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n^2)
|
||||
|
||||
###### View the algorithm in [action][insertion-toptal]
|
||||
|
||||
|
||||
### Merge
|
||||
![alt text][merge-image]
|
||||
|
||||
From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n log n)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n)
|
||||
|
||||
|
||||
###### View the algorithm in [action][merge-toptal]
|
||||
|
||||
|
||||
|
||||
### Selection
|
||||
![alt text][selection-image]
|
||||
|
||||
From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n^2)
|
||||
* Average case performance O(n^2)
|
||||
|
||||
###### View the algorithm in [action][selection-toptal]
|
||||
|
||||
|
||||
|
||||
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
|
||||
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
|
||||
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
|
||||
|
||||
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
|
||||
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
|
||||
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
|
||||
|
||||
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
|
||||
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
|
||||
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"
|
||||
|
||||
[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort
|
||||
[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort
|
||||
[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort"
|
||||
|
||||
[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort
|
||||
[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort
|
||||
[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort"
|
||||
|
||||
[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort
|
||||
[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort
|
||||
[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort"
|
||||
|
||||
[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search
|
||||
[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif
|
||||
|
||||
[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm
|
||||
[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png
|
||||
|
||||
|
||||
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg
|
||||
|
Loading…
Reference in New Issue
Block a user