Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting (names) using Merge Sort

having problem sorting repeated Strings,

and here's my code..

i successfully sorted the first array but in the second (with repeated strings) it seems not in orderly output, can you help me to trace whats wrong in my code..

import java.util.*;

public class NewClass {
    public static void main(String[] args) {
        String[] ClassOne = { "Kring", "Panda", "Soliel", "Darryl", "Chan", "Matang", "Jollibee.", "Inasal" };
        String[] ClassTwo = { "Minnie", "Kitty", "Madonna", "Miley", "Zoom-zoom", "Cristine", "Bubbles", "Ara", "Rose", "Maria" };
        String[] names = new String[ClassOne.length + ClassTwo.length];

        mergeSort(ClassOne);
        mergeSort(ClassTwo);

        merge(names, ClassOne, ClassTwo);

        mergeSort(names);
        //Arrays.sort(names);

        for (String ClassThree : names) {
            System.out.println(ClassThree);
        }
    }

    public static void mergeSort(String[] names) {
        if (names.length > 2) {
            String[] left = new String[names.length / 2];
            String[] right = new String[names.length - names.length / 2];

            for (int i = 0; i < left.length; i++) {
                left[i] = names[i];
            }

            for (int i = 0; i < right.length; i++) {
                right[i] = names[i + names.length / 2];
            }

            mergeSort(left);
            mergeSort(right);
            merge(names, left, right);
        }
    }

    public static void merge(String[] names, String[] left, String[] right) {
        int a = 0;
        int b = 0;
        for (int i = 0; i < names.length; i++) {
            if (b >= right.length || (a < left.length && left[a].compareToIgnoreCase(right[b]) < 0)) {
                names[i] = left[a];
                a++;
            } else {
                names[i] = right[b];
                b++;
            }
        }
    }
}

and heres the output::

Ara
Chan
Cristine
Bubbles
Jollibee.
Inasal
Kring
Madonna
Matang
Miley
Minnie
Kitty
Panda
Rose
Maria
Soliel
Darryl
Zoom-zoom

...

like image 985
jarnthrax Avatar asked Dec 27 '13 05:12

jarnthrax


People also ask

Which method is used for sorting in merge sort?

Explanation: Merge sort uses divide and conquer in order to sort a given array. This is because it divides the array into two halves and applies merge sort algorithm to each half individually after which the two sorted halves are merged together.

Can you merge sort a list?

function will sort a linked list using the merge sort algorithm. to cut the list from the middle node into two halves. Eventually, we sort each part separately, then we'll merge them to get a single sorted list.


1 Answers

Change

if (names.length > 2) {

with

if (names.length >= 2) {

output

Ara
Bubbles
Chan
Cristine
Darryl
Inasal
Jollibee.
Kitty
Kring
Madonna
Maria
Matang
Miley
Minnie
Panda
Rose
Soliel
Zoom-zoom
like image 55
Ashish Aggarwal Avatar answered Sep 21 '22 23:09

Ashish Aggarwal