Home Answers Viewqa Java-Beginners Merge Sort String Array in Java

 
 


Sergios Schizas
Merge Sort String Array in Java
1 Answer(s)      a year and a month ago
Posted in : Java Beginners

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 Pages:
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
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
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
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 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
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
Java: Example - String sort
Java: Example - String sort Sorting is a mechanism in which we sort the data...() // Sort a String array using selection sort. void sort(String... the string. The example given below is based on Selection Sort. The Selection sort
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
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
string array sort
string array sort  Hi. How to sort a string array
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
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
Sort
with this A program is required to ask users to rate the Java programming language... RateJava { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Rate Java(0-10): "); int rate
Insertion Sort In Java
Insertion Sort In Java      ... such as quick sort, heap sort, or merge sort for large values . Positive... C:\array\sorting>java InsertionSort RoseIndia
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
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
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
bubble sort - Java Beginners
bubble sort  how to write program The bubble-sort algorithm in double...  Hi friend, Bubble Sort program : public class BubbleSortExam{ public static void main(String a[]){ int i; int array
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
Java insertion sort question
Java insertion sort question  I've got another program that I need help with. I am trying to write a Java method that accepts an array of strings... a main method to create a string array with 5 city names (e.g. Atlanta, New York
Insertion Sort - Java Beginners
: public class InsertionSort { public static void sort(String[] array) { int...) { String[] array ={"S","D", "A","B","Z", "M","O", "L","H", "Y"}; sort...{ public static void main(String a[]){ String array[] = {"S","D", "A","B","Z
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:  General Format array array_merge(array
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
sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string
heap sort in java
heap sort in java  plz modify this program so that it can take input as integers and string both..... public class heap_Sort{ public static void...("\n Heap Sort\n---------------\n"); System.out.println("\n Unsorted Array\n\n
array string
array string  how to sort strings with out using any functions
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
PHP Merge Array
PHP Merge Array  How to merge array's in PHP
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_merge null - PHP
array_merge null   i am using the array_merge and getting the unusual null error...any help
Array sort
Array sort  Program that uses a function to sort an array of integers
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
Sort
Sort  program to sort a list of numbers in decendimg order   Hi Friend, Try the following code: import java.util.*; class SortListInDescendingOrder{ public static void main(String[] args
Bubble Sort in Java
of Bubble Sort in Java: public class BubbleSort { public static void main(String...Bubble Sort aka exchange sort in Java is used to sort integer values... Sort compares first pair of adjacent elements and put larger value at higher
quick sort
the sort:\n"); for(i = 0; i < array.length; i++){ array[i... answer "array based problem" for run time input.but i am facing some problem.plz... static void quick_srt(int array[],int low, int n){ int lo = low; int hi
quick sort
the sort:\n"); for(i = 0; i < array.length; i++){ array[i... answer "array based problem" for run time input.but i am facing some problem.plz... static void quick_srt(int array[],int low, int n){ int lo = low; int hi
quick sort
the sort:\n"); for(i = 0; i < array.length; i++){ array[i... answer "array based problem" for run time input.but i am facing some problem.plz... static void quick_srt(int array[],int low, int n){ int lo = low; int hi
C array sort example
C array sort example       In this section, you will learn how to sort an array in C. For sorting... the implementation of quicksort algorithm to sort the elements of an array. Syntax
Java arraylist merge
is joined in to the list1. Example Java Arraylist Merge import java.util.*; public class array3 {     public static void main(String[] args... In Java Two arrays can be joined by using the Collection list
Insertion Sort Java
Insertion Sort in Java is an algorithm that is used to sort integer values..., Insertion Sort in Java is less efficient when it comes to larger data sets... decreasing. How does Insertion Sort works in Java? Assume that you have 5
Selection Sort In Java
Selection Sort In Java      ... same steps. Working of the selection sort :Say we have an array... C:\array\sorting>java selectionSort RoseIndia
Quick Sort In Java
Quick Sort in Java      ...: In quick sort algorithm pick an element from array...;java QuickSort RoseIndia Quick Sort
JavaScript array functions sort
. This method sort the element by alphabetically. The sorted array assigned to a string... JavaScript array functions sort   ... to understand the concept of JavaScript array function sort. The code create
complete this code (insertion sort) - Java Beginners
static void main(String[] args) { // data int[] input; /* array of numbers to sort */ Scanner sc; /* for reading user input */ String line...] = Integer.parseInt(numbers[i]); // 3. Sort the array int [] sorted
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.