Total members 11890 |It is currently Fri Apr 19, 2024 6:51 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Java merge sort example
java code
 
package sort;

 
public class MergeSort {

    public static void main(String a[]) {


        int nonSortedArray[] = {532, 4, 2, 18, 354, 25, 17, 51, 3};

        // Printing the array
        System.out.println(" Before Sorting:");
        for (int i = 0; i < nonSortedArray.length; i++) {
            System.out.print(nonSortedArray[i] + " , ");
        }
        System.out.println();
        // Merging Sort
        for (int i = 1; i < nonSortedArray.length; i *= 2) {

            for (int j = 0; j < nonSortedArray.length - i; j += 2 * i) {
                int subArrayToBeSortedSize = (2 * i < nonSortedArray.length - j) ? 2 * i : nonSortedArray.length - j;
                nonSortedArray = MergeSort(nonSortedArray, j, i, subArrayToBeSortedSize);
            }
        }

        // Output sorted array
        System.out.println("After sorting:");
        for (int i = 0; i < nonSortedArray.length; i++) {
            System.out.print(nonSortedArray[i] + "  ");
        }
        System.out.println();


    }

    public static int[] MergeSort(int[] myArray, int startIndex, int firstArrayLimit, int secondArrayLimit) {


        int i = 0 + startIndex;
        int j = firstArrayLimit + startIndex;
        int k = 0;
        int[] tempArray = new int[secondArrayLimit];
        // Compare the two arrays elements
        while (i < firstArrayLimit + startIndex && j < secondArrayLimit + startIndex) {
            if (myArray[i] < myArray[j]) {
                tempArray[k] = myArray[i];
                ++i;
            } else {
                tempArray[k] = myArray[j];
                ++j;
            }
            ++k;
        }
        // Copy the remaining elements of the 1st array if exists
        while (i < firstArrayLimit + startIndex) {
            tempArray[k] = myArray[i];
            ++i;
            ++k;
        }
        // Copy the remaining elements of the 2nd array if exists
        while (j < secondArrayLimit + startIndex) {
            tempArray[k] = myArray[j];
            ++j;
            ++k;
        }
        // Copy the merged part of the array
        for (int iIndex = 0; iIndex < secondArrayLimit; ++iIndex) {
            myArray[iIndex + startIndex] = tempArray[iIndex];

        }


        return myArray;
    }
}

 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Java merge sort example
 merge sort implementation in C++     -  
 Java Insertion Sort Code     -  
 bidirectional bubble sort algorithm implementation java     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 lists merge     -  
 Merge two or more arrays recursively     -  
 multidimensional array merge using PHP     -  
 How to merge the data into file     -  
 Sort strings java-Sorting Array of Strings     -  
 Insertion Sort (C++)     -  



Topic Tags

Java Algorithms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com