Skip to content

Commit

Permalink
merge_sort.java Add MergeSort Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Sassone authored and sangamcse committed Oct 29, 2018
1 parent 94e52aa commit 2aca28e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This repository contains examples of various algorithms written on different pro
| Algorithm | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | | | [:octocat:](merge_sort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | |[:octocat:](merge_sort/Java) | [:octocat:](merge_sort/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | | [:octocat:](quicksort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) |[:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
Expand Down
64 changes: 64 additions & 0 deletions merge_sort/Java/merge_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.Arrays;

class merge_sort {

//Main function. Instanciates the unsorted array and calls mergesort.
public static void main(String args[]) {

int intArray[] = {9, 5, 4, 6, 1, 2, 3, 1, 2, 3};
System.out.println("The unsorted array is:");
System.out.println(Arrays.toString(intArray));

mergesort(intArray);

System.out.println("The sorted array is:");
System.out.println(Arrays.toString(intArray));
}

//Splits array by half, creating two new arrays.
//Calls mergesort recursively on both of them.
//(they are expected to be returned sorted)
//then unifies them on the initial array calling merge.
static void mergesort(int intArray[]) {
int size = intArray.length;
if (size < 2) return;
int mid = size / 2;
int left[] = new int[mid];
int right[] = new int[size - mid];
for (int i = 0; i < mid; i++) left[i] = intArray[i];
for (int i = mid; i < size; i++) right[i - mid] = intArray[i];
mergesort(left);
mergesort(right);
merge(intArray, left, right);
}

//Unifies left and right on array.
//Both of them are expected to be already sorted.
//Goes through every index of left and right
//comparing values and saves the lesser one on array.
public static void merge(int array[], int left[], int right[]) {
int sizeLeft = left.length;
int sizeRight = right.length;
int i = 0, j = 0, k = 0;
while (i < sizeLeft && j < sizeRight) {
if (left[i] <= right[j]) {
array[k] = left[i];
i++;
} else {
array[k] = right[i];
j++;
}
k++;
}
while (i < sizeLeft) {
array[k] = left[i];
i++;
k++;
}
while (j < sizeRight) {
array[k] = right[j];
j++;
k++;
}
}
}

0 comments on commit 2aca28e

Please sign in to comment.