From 8e367a7c0e2d38d7048fe2409075f305da1c2489 Mon Sep 17 00:00:00 2001 From: Bhashitha Dhananjaya <44166845+glbdhananjaya@users.noreply.github.com> Date: Wed, 23 Oct 2019 11:18:34 +0530 Subject: [PATCH] added merge sort algorithms using java --- Sorting Algorithms/Merge_sort (java alg).java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Sorting Algorithms/Merge_sort (java alg).java diff --git a/Sorting Algorithms/Merge_sort (java alg).java b/Sorting Algorithms/Merge_sort (java alg).java new file mode 100644 index 0000000..254c04f --- /dev/null +++ b/Sorting Algorithms/Merge_sort (java alg).java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package merge_sort; + +import java.util.Arrays; + +/** + * + * @author Bhashitha + */ +public class Merge_sort { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + //Unsorted array + Integer[] a = { 2, 6, 3, 5, 1 }; + + //Call merge sort + mergeSort(a); + + //Check the output which is sorted array + System.out.println(Arrays.toString(a)); + } + + @SuppressWarnings("rawtypes") + public static Comparable[] mergeSort(Comparable[] list) + { + //If list is empty; no need to do anything + if (list.length <= 1) { + return list; + } + + //Split the array in half in two parts + Comparable[] first = new Comparable[list.length / 2]; + Comparable[] second = new Comparable[list.length - first.length]; + System.arraycopy(list, 0, first, 0, first.length); + System.arraycopy(list, first.length, second, 0, second.length); + + //Sort each half recursively + mergeSort(first); + mergeSort(second); + + //Merge both halves together, overwriting to original array + merge(first, second, list); + return list; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private static void merge(Comparable[] first, Comparable[] second, Comparable[] result) + { + //Index Position in first array - starting with first element + int iFirst = 0; + + //Index Position in second array - starting with first element + int iSecond = 0; + + //Index Position in merged array - starting with first position + int iMerged = 0; + + //Compare elements at iFirst and iSecond, + //and move smaller element at iMerged + while (iFirst < first.length && iSecond < second.length) + { + if (first[iFirst].compareTo(second[iSecond]) < 0) + { + result[iMerged] = first[iFirst]; + iFirst++; + } + else + { + result[iMerged] = second[iSecond]; + iSecond++; + } + iMerged++; + } + //copy remaining elements from both halves - each half will have already sorted elements + System.arraycopy(first, iFirst, result, iMerged, first.length - iFirst); + System.arraycopy(second, iSecond, result, iMerged, second.length - iSecond); + } +} +