Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: MergeSort not working Helppp please :(

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MergeSort not working Helppp please :(

    I've been working on this mergeSort that has to read from a file then give me the size and then print out the sorted array. However it's giving me the size and printing out the list without sorting it... Helppp please

    Here's my code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.InputMismatchException;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
     
    /**
     *
     * @author fluffy
     */
    public class Numbers {
     
        int[] data;
        int size;
     
        Numbers(String fileName) throws IOException {
            data = openFile(fileName);
            data = mergeSort(data);
     
            for (int i = 0; i < data.length; i = i + 1) {
                System.out.println("*" + data[i]);
            }
        }
     
        public int[] openFile(String fileName) throws
                IOException, ArrayIndexOutOfBoundsException,
                InputMismatchException, NoSuchElementException {
     
            int[] a = new int[10];
            int i = 0;
            int k = 0;
            try {
                File inFile = new File(fileName);
                Scanner in = new Scanner(inFile);
     
                //while(in.hasNext()){
                do {
     
                    try {
                        k = in.nextInt();
     
                        try {
     
                            a[i] = k;
                        } catch (ArrayIndexOutOfBoundsException e) {
                            System.out.println("Array bound error");
                            a = grow(a);
                            a[i] = k;
                        }
                        i = i + 1;
                    } catch (InputMismatchException e) {
                        in.next();
                    }
                } while (true);
     
     
            } catch (IOException e) {
                //System.out.println("File 2 not found in openFile3  " + e.getMessage());
            } catch (NoSuchElementException e) {
                size = i - 1;
            }
     
            System.out.println("Size of the array " + size);
     
            return a;
        }
     
        private static int[] grow(int[] a) {
            int[] b = new int[(a.length) * 2];
            for (int i = 0; i <
                    a.length; i =
                            i + 1) {
                b[i] = a[i];
            }
     
            return b;
     
        }
     
        public static int[] mergeSort(int[] data) {
            if (data.length > 1) {
     
                int mid = data.length / 2;
     
                int[] first = new int[mid];
                int[] second = new int[data.length - mid];
     
                for (int i = 0; i < first.length; i = i + 1) {
                    first[i] = data[i];
                }
                for (int i = 0; i < second.length; i = i + 1) {
                    second[i] = data[i + mid];
                }
     
                first = mergeSort(first);
                second = mergeSort(second);
                data = merge(first, second);
     
            }
     
            return data;
        }
     
     
        public static int[] merge(int[] first, int[] second) {
            int i = 0, j = 0;
            int k = 0;
            int[] mergeData = new int[first.length + second.length];
     
            if (i < first.length && j < second.length) {
                if (first[i] < second[j]) {
                    mergeData[k] = first[i];
                    i = i + 1;
                    k = k + 1;
                } else {
                    mergeData[k] = second[j];
                    j = j + 1;
                    k = k + 1;
                }
     
                for (; i < first.length; i = i + 1) {
                    mergeData[k] = first[i];
                    k = k + 1;
                }
                for (; j < second.length; j = j + 1) {
                    mergeData[k] = second[j];
                    k = k + 1;
                }
     
     
            }
            return mergeData;
        }
    }
    Last edited by helloworld922; May 16th, 2010 at 07:21 PM. Reason: please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: MergeSort not working Helppp please :(

    Your MergeSort method never returns the sorted list.

        public static int[] mergeSort(int[] data) {
            if (data.length > 1) {
     
                int mid = data.length / 2;
     
                int[] first = new int[mid];
                int[] second = new int[data.length - mid];
     
                for (int i = 0; i < first.length; i = i + 1) {
                    first[i] = data[i];
                }
                for (int i = 0; i < second.length; i = i + 1) {
                    second[i] = data[i + mid];
                }
     
                first = mergeSort(first);
                second = mergeSort(second);
                data = merge(first, second);
                [b]return data;[/b] // need this line
            }

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MergeSort not working Helppp please :(

    it does have the return data; after that } because if i put it where u placed it at the beggining its going to tell me im missing the return statement... i tried it =/

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: MergeSort not working Helppp please :(

    The sorting code is buggy - it does partially sort the list, but not fully.

    I suggest you pick a trivial set of values and step through each method by hand keeping track of what happens on paper.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: MergeSort not working Helppp please :(

    Ahh, didn't look through it that closely ::blush::
    dlorde's right, there is a flaw in your merge method. Do what he says and see if you can figure out what it is

Similar Threads

  1. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM
  2. MergeSort
    By mgutierrez19 in forum Algorithms & Recursion
    Replies: 3
    Last Post: March 3rd, 2010, 08:46 PM
  3. Working with MAC .DMG files
    By mdv2000 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 21st, 2010, 12:31 PM
  4. Problem in import com. in eclipse
    By java_cs in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 11th, 2009, 07:26 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM