Skip to content

Commit aea0283

Browse files
committed
Updated Readme and Implemented Merge Sort
1 parent a111922 commit aea0283

File tree

2 files changed

+204
-2
lines changed

2 files changed

+204
-2
lines changed

MergeSort.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Merge Sort
5+
*
6+
*/
7+
public class MergeSort {
8+
private int[] array;
9+
private int[] tempMergArr;
10+
private int length;
11+
12+
/**
13+
* Sorts {@code inputArr} with merge sort algorithm.
14+
*
15+
* @param inputArr
16+
*/
17+
public final void sort(int inputArr[]) {
18+
this.array = inputArr;
19+
this.length = inputArr.length;
20+
this.tempMergArr = new int[this.length];
21+
this.mergeSort(0, this.length - 1);
22+
}
23+
24+
/**
25+
* Partitions Array into recursively smaller pieces.
26+
*
27+
* @param lowerIndex
28+
* lower bound to include in the first partition
29+
* @param higherIndex
30+
* upper bound to include in the third partition
31+
*/
32+
private void mergeSort(int lowerIndex, int higherIndex) {
33+
if (lowerIndex < higherIndex) {
34+
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
35+
// Below step sorts the left side of the array
36+
this.mergeSort(lowerIndex, middle);
37+
// Below step sorts the right side of the array
38+
this.mergeSort(middle + 1, higherIndex);
39+
// Now merge both sides
40+
this.mergeParts(lowerIndex, middle, higherIndex);
41+
}
42+
}
43+
44+
/**
45+
* Merges partitions.
46+
*
47+
* @param lowerIndex
48+
* @param middle
49+
* @param higherIndex
50+
*/
51+
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
52+
for (int i = lowerIndex; i <= higherIndex; i++) {
53+
this.tempMergArr[i] = this.array[i];
54+
}
55+
int i = lowerIndex;
56+
int j = middle + 1;
57+
int k = lowerIndex;
58+
while (i <= middle && j <= higherIndex) {
59+
if (this.tempMergArr[i] <= this.tempMergArr[j]) {
60+
this.array[k] = this.tempMergArr[i];
61+
i++;
62+
} else {
63+
this.array[k] = this.tempMergArr[j];
64+
j++;
65+
}
66+
k++;
67+
}
68+
while (i <= middle) {
69+
this.array[k] = this.tempMergArr[i];
70+
k++;
71+
i++;
72+
}
73+
74+
}
75+
76+
/**
77+
* Gets input to sort.
78+
*
79+
* @return unsorted array of integers to sort
80+
*/
81+
public static int[] getInput() {
82+
final int numElements = 6;
83+
int[] unsorted = new int[numElements];
84+
Scanner input = new Scanner(System.in);
85+
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
86+
for (int i = 0; i < numElements; i++) {
87+
unsorted[i] = input.nextInt();
88+
}
89+
input.close();
90+
return unsorted;
91+
}
92+
93+
/**
94+
* Main Method.
95+
*
96+
* @param args
97+
*/
98+
public static void main(String args[]) {
99+
int[] inputArr = getInput();
100+
MergeSort mergeSort = new MergeSort();
101+
mergeSort.sort(inputArr);
102+
for (int i : inputArr) {
103+
System.out.print(i);
104+
System.out.print(" ");
105+
}
106+
}
107+
}

README.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
1-
# Java
2-
All Algorithms implemented in Java
1+
# The Algorithms - Java [![Build Status](https://travis-ci.org/TheAlgorithms/Python.svg)](https://travis-ci.org/TheAlgorithms/Python)
2+
3+
### All algorithms implemented in Java (for education)
4+
5+
These are for demonstration purposes only. There are many implementations of sorts in the Java standard library that are much better for performance reasons.
6+
7+
## Sort Algorithms
8+
9+
10+
### Bubble
11+
![alt text][bubble-image]
12+
13+
From [Uncyclopedia][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.
14+
15+
__Properties__
16+
* Worst case performance O(n^2)
17+
* Best case performance O(n)
18+
* Average case performance O(n^2)
19+
20+
###### View the algorithm in [action][bubble-toptal]
21+
22+
23+
24+
### Insertion
25+
![alt text][insertion-image]
26+
27+
From [Uncyclopedia][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.
28+
29+
__Properties__
30+
* Worst case performance O(n^2)
31+
* Best case performance O(n)
32+
* Average case performance O(n^2)
33+
34+
###### View the algorithm in [action][insertion-toptal]
35+
36+
37+
### Merge
38+
![alt text][merge-image]
39+
40+
From [Uncyclopedia][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.
41+
42+
__Properties__
43+
* Worst case performance O(n log n)
44+
* Best case performance O(n)
45+
* Average case performance O(n)
46+
47+
48+
###### View the algorithm in [action][merge-toptal]
49+
50+
51+
52+
### Selection
53+
![alt text][selection-image]
54+
55+
From [Uncyclopedia][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.
56+
57+
__Properties__
58+
* Worst case performance O(n^2)
59+
* Best case performance O(n^2)
60+
* Average case performance O(n^2)
61+
62+
###### View the algorithm in [action][selection-toptal]
63+
64+
65+
66+
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
67+
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
68+
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
69+
70+
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
71+
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
72+
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
73+
74+
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
75+
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
76+
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"
77+
78+
[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort
79+
[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort
80+
[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort"
81+
82+
[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort
83+
[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort
84+
[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort"
85+
86+
[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort
87+
[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort
88+
[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort"
89+
90+
[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search
91+
[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif
92+
93+
[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm
94+
[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png
95+
96+
97+
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg

0 commit comments

Comments
 (0)