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;
    }


}









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... sorted list remaining. Example of Merge Sort in Java public class... mergeSort.java C:\array\sorting>java mergeSort RoseIndia Selection Sort Values
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 
array sort - Java Beginners
array sort  hi all, can anybody tell me how to sort an array...; } } public static void main(String a[]){ int i; int array... array[], int len){ for (int i = 1; i < len; i++){ int j = i
Java Merge Array
Java Merge Array You all are aware of Arrays, their structure, declaration... into a single array. In this section, we are going to explain you this concept with an example. In the given code, we have created a method 'merge()' where we have
Array sort
Array sort  Program that uses a function to sort an array of integers
PHP Merge Array
PHP Merge Array  How to merge array's in PHP
sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string
array_merge null - PHP
array_merge null   i am using the array_merge and getting the unusual null error...any help
sorting an array of string with duplicate values - Java Beginners
sorting an array of string with duplicate values  I have a sort method which sorts an array of strings. But if there are duplicates in the array it would not sort properly
String Array - Java Beginners
String Array  From where can I get, all the functions that are needed for me to manipulate a String Array. For Example, I had a String Array ("3d4..., as to by which method can I separate the Integers from this Array of String
php array sort by field
php array sort by field  Array sort by field in PHP
php array sort by key
php array sort by key  php script to sort array by key
php array sort by value
php array sort by value  an example to sort the array by value
String Array - Java Beginners
for me to manipulate a String Array. For Example, I had a String Array ("3d4..., as to by which method can I separate the Integers from this Array of String... this question to you before, now the problem comes if my String Array consisted
php array sort functions
php array sort functions  Sort function in php
array_merge null - PHP
array_merge null   i am using the array_merge and getting the unusual... links: http://www.roseindia.net/tutorial/php/phpbasics/PHP-Array-Merge-Recursive.html http://www.roseindia.net/tutorial/php/phpbasics/PHP-Array-Merge.html
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
java String array - Java Beginners
java String array  I want to print values stored in array of string ("1","2","3","4","5" ...) in the form 1 2 3 4 5 6 7 8 9 10 11 12 how... { public static void main(String[] args) { int k=0; String str[]={"1","2
Heap Sort in Java
Heap Sort in Java is used to sort integer values of an array. Like quicksort...:\array\sorting>java heap_Sort Heap Sort --------------- Unsorted Array 1 3... values in heap is not 0. Example of Heap Sort in Java: public class eap
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
PHP Array Merge
PHP Array Merge In many situations we have to merge two or more than two arrays, we need to use array_merge() function. This function merges or append... of array_merge() is: ADS_TO_REPLACE_1 General Format array
Selection Sort in Java
Selection sort in Java is used to sort the unsorted values in an array... Sort in Java: public class selectionSort{ public static void main(String... the selection sort in Java. In selection sort algorithm, first assign minimum index
PHP Array Merge
In php two or more arrays can be added into a single array It is done by the use of array_merge() function Example of PHP Array Merge Function <?php $array1=array(1,2,3); $array2=array(4,5,6); $array3=array_merge
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
PHP Array Merge Recursive
PHP Array Merge Recursive The PHP array_merge_recursive() function is same as the array_merge() function. It creates an array by appending each input array to the previous array. The main Difference between array_merge() and array_merge
array to string
array to string  hello how to assign value from array to string. nsstring *abc = [array objectAtindex:1];   you can use this codeADS_TO_REPLACE_1 NSString *abc = [NSString stringWithString:[array objectAtIndex:i
JSP to output Java String Array - JSP-Servlet
JSP to output Java String Array  I am just a little confused about the output that I would get from printing a 2D String array loaded with database fields. For example lets say we have the following array: String [ ][ ] array
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     
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
Quick Sort in Java
Quick sort in Java is used to sort integer values of an array... into a sorted array. ADS_TO_REPLACE_1 Example of Quick Sort in Java: public class QuickSort{ public static void main(String a[]){ int i; int 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
merge sorting in arrays - Java Beginners
merge sorting in arrays  Write a program to insert string or characters to an array and apply merge sorting on this array  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners
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
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
sorting an array of string with duplicate values - Java Beginners
String of Array  What is mean by string of array? And how one can add, delete the records from database in string format
php array sort
PHP Array sort function is used to sort the given array in ascending order. Example of PHP Array Sort <?php $ar1=array("jack","mac","rock","barak");ADS_TO_REPLACE_1 sort($ar1); foreach ($ar1 as $a) echo " ".$a;ADS
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
PHP Array Sort in Reverse Order
PHP Array Sort in Reverse Order  Hi, I have just started learning... the PHP Array Sort in Reverse Order. Please suggest any online reference or example... programming language there are three types of functions used to sort the array

Ads