Merge Sort String Array in Java

Merge Sort String Array in Java

Hello, I am trying to implement a merge sort algorithm that sorts an array of Strings. I have seen numerous examples of merge-sorting integers but i can not understand how to this with String. Note: I have a separate class for merge sort in which i pass my list and the size of the list. i then copy my list in the array. the method call inside the main looks like this: merge.Sort(list, list.size()) ;.

Any help would be appreciated. Thank you

View Answers

April 21, 2012 at 11:44 AM

public class MergeTestStringArray {
    public static void main(String[] args) {
        String[] array = { "John", "Anthony", "Angelina", 
                "George", "Victor", "Joe", "Jackson" };

            String[] sortedArray = mergeSort(array);
            for (int i = 0; i < sortedArray.length; i++) {
            System.out.println(sortedArray[i] + " ");
            }

    }

    public static String[] mergeSort(String[] list) {
        String [] sorted = new String[list.length];
        if (list.length == 1) {
            sorted = list;
        } else {
            int mid = list.length/2;
            String[] left = null; 
            String[] right = null;
            if ((list.length % 2) == 0) {
                left = new String[list.length/2];
                right = new String[list.length/2];
            } else { 
                left = new String[list.length/2];
                right = new String[(list.length/2)+1];
            }
            int x=0;
            int y=0;
            for ( ; x < mid; x++) {
                left[x] = list[x];
            }
            for ( ; x < list.length; x++) {
                right[y++] = list[x];
            }
            left = mergeSort(left);
            right = mergeSort(right);
            sorted = mergeArray(left,right);
        }

        return sorted;
    }

    private static String[] mergeArray(String[] left, String[] right) {
        String[] merged = new String[left.length+right.length];
        int lIndex = 0;
        int rIndex = 0;
        int mIndex = 0;
        int comp = 0;
        while (lIndex < left.length || rIndex < right.length) {
            if (lIndex == left.length) {
                merged[mIndex++] = right[rIndex++];
            } else if (rIndex == right.length) {
                merged[mIndex++] = left[lIndex++];
            } else {  
                comp = left[lIndex].compareTo(right[rIndex]);
                if (comp > 0) {
                    merged[mIndex++] = right[rIndex++];
                } else if (comp < 0) {
                    merged[mIndex++] = left[lIndex++];
                } else { 
                    merged[mIndex++] = left[lIndex++];
                }
            }   
        }
        return merged;
    }


}

Ads









Related Tutorials/Questions & Answers:
Merge Sort String Array in Java
Merge Sort String Array in Java  Hello, I am trying to implement a merge sort algorithm that sorts an array of Strings. I have seen numerous examples of merge-sorting integers but i can not understand how to this with String
string array sort
string array sort  Hi. How to sort a string array
Advertisements
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
Merge Sort Java
Merge Sort in Java is used to sort integer values of an array. There are many methods to sort Java like bubble sort, insertion sort, selection sort, etc.... Example of Merge Sort in Java public class mergeSort{ public static void main
Java insertion sort with string array
Java insertion sort with string array In this tutorial, you will learn how to sort array of strings using string array with Insertion Sort. For this, we...++){ System.out.println(sortedArray[i]); } } public static String[] sort_sub(String array[], int
Merge Sort In Java
Merge Sort in Java      ... are followed by merge sort algorithm to sort the values of an array. Step1:Spliting.... Steps of Merge Sort: Say unsorted  an array values
String array sort
String array sort  Hi here is my code. If i run this code I am... language="java"%> <%@ page session="true"%> <% Connection... result_set=null; String route_number[]=new String[1000]; String
String array sort
String array sort  Hi here is my code. If i run this code I am... language="java"%> <%@ page session="true"%> <% Connection... result_set=null; String route_number[]=new String[1000]; String
Extra Storage Merge Sort in Java
Extra Storage Merge Sort in Java   ... into an array .Then again merge the next part , sort it and store into an array. Do... storage merge sort algorithm: Say we have an array unsorted 
String Array - Java Beginners
String Array  Thanks for the help you had provided,,, Your solution did worked... I worked on the previous solution given by you and it worked.... I... again,,, and I'll come back to you , if I had other problem regarding JAVA
array string
array string  how to sort strings with out using any functions
String Array In Java
String Array In Java       In this section, you will learn how to use string array in Java. Here, you will see how to declare a string array and the syntax for using in the program
Heap Sort in Java
Heap Sort in Java is used to sort integer values of an array. Like quicksort...\sorting>Javac heap_Sort.java C:\array\sorting>java heap_Sort Heap Sort... are replaced into array. Steps in heap sort: The Root node is replaced
Selection Sort in Java
Selection sort in Java is used to sort the unsorted values in an array... in Java. In selection sort algorithm, first assign minimum index in key as index... the whole list is sorted. Example of Selection Sort in Java: public class
string array
string array   Hi hw to print string array element in ascending r... StringArray { public static void main(String[] args) { String arr...); System.out.println("Array Elements in Ascending Order: "); for(int i=0;i<
how to convert string to char array in Java
how to convert string to char array in Java  Hi, I have a string in Java which has to be converted to char array. What are the options to convert String object to char Array in Java? how to convert string to char array
array of string
array of string  Waht is the problem in this code? import java.util.Scanner; public class LastCheck { public static void main(String args[]){ int a; Scanner s= new Scanner(System.in); a=s.nextInt(); String ar[]=new
How to convert Arraylist into String Array Java
into string array using java language. Please visit the below link: http...How to convert Arraylist into String Array Java  Hi, I am beginners of Java programming. Can somebody Suggest me how to convert arraylist to string
Quick Sort in Java
Quick sort in Java is used to sort integer values of an array... into a sorted array. Example of Quick Sort in Java: public class QuickSort... QuickSort.java C:\array\sorting>java QuickSort RoseIndia Quick Sort
string array based problem
string array based problem  R/sir, firstly thanks to help me so much. but now it can sort string very well but in case of integers... string: "); String[] array = new String[5]; for(int i=0;i<5;i++){ array[i
array_merge function in php - PHP
array_merge function in php  What is the best use of array_merge...://www.roseindia.net/tutorial/php/phpbasics/PHP-Array-Merge-Recursive.html http://www.roseindia.net/tutorial/php/phpbasics/PHP-Array-Merge.html http
JavaScript array merge
in understanding how to merge two different array into one array using Java Script. We declare a array variable that is used to store array object. An array... JavaScript array merge     
array and string based problem
array and string based problem  this program is accepting only... main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new..."); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++){ array
array and string based problem
array and string based problem  this program is accepting only... main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new..."); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++){ array
array and string based problem
array and string based problem  this program is accepting only... main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new..."); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++){ array
String sort() Method
String sort() Method       In this program you will learn how to sort words in a String... of String class in Java. The description of the code is given below for the usage
How to declare String array in Java?
Following example will show you how to declare string array in java... elements of one dimensional array and two dimensional array. 1. String arr... and initialize one dimension array String[] names = new String[]{"Somendra
Convert a string representation of a hex dump to a byte array using Java?
Convert a string representation of a hex dump to a byte array using Java?  Convert a string representation of a hex dump to a byte array using Java

Ads