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 3 of 3

Thread: Merge Sort

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Merge Sort

    I have a lab that requires me to sort type T arrays with assertion sort and merge sort. But my merge sort is giving me some problems and I'm not sure why. Thank you in advance for any help!

    ArraySort.java

     
    /**
     * @param <T>
     *
     */
    public class ArraySort<T> {
     
    	/**
    	 * @param a
    	 * @param length
    	 */
    	public static<T extends Comparable<T>> void insertionSort(T[] a, int length){
    		int j;
    		T temp; 
    		for(int i=1;i<length;i++){
    			temp = a[i]; 
    			j=i;
    				while(j>0 && (a[j-1].compareTo(temp)>0)) { 
    					a[j] = a[j-1];
    					j--; 
    				}
    			a[j] = temp; 
    		} 
    	}
     
     
    	/**
    	 * @param a
    	 * @param length
    	 */
    	@SuppressWarnings("javadoc")
    	public static<T extends Comparable<T>> void mergeSort(T[] a, int length){
     
    			SortMerge(a, length, 0, length-1);
     
    	}
     
    	/**
    	 * @param a
    	 * @param length
    	 * @param first
    	 * @param last 
    	 */
    	@SuppressWarnings("javadoc")
    	private static<T extends Comparable<T>> void SortMerge(T[] a, int length, int first, int last ){
    		if(first < last){
    			int middle = first + (last-first)/2;
    			SortMerge(a, length, first, middle);
    			SortMerge(a, length, middle+1, last);
    			merge(a, length, first, middle, last);
    		}
    	}
     
    	/**
    	 * @param a
    	 * @param length
    	 * @param first
    	 * @param middle
    	 * @param last
    	 */
    	@SuppressWarnings({ "null", "javadoc" })
    	private static<T extends Comparable<T>> void merge(T[] a, int length, int first, int middle, int last){
    		int i = first;
    		int j = middle + 1;
    		int k = first;
    		T[] temp = null;
    		for (int x = first; x < length; x++){
    			temp[x] = a[x];
    		}
    		while(i <= middle && j <= last){
    			if(temp[i].compareTo(temp[j]) > 0){
    				a[k] = temp[i];
    				i++;
    			}
    			else{
    				a[k] = temp[k];
    				j++;
    			}
    			k++;
    		}
    		while(i <= middle){
    			a[k]=temp[i];
    			k++;
    			i++;
    		}
    	}
    }

    and the driver just in case you needed it

    SortDriver.java
     
    public class SortDriver {
    	/**
    	 * @param args
    	 */
    	@SuppressWarnings("javadoc")
    	public static void main(String[] args){
    	String[] test1 = {"a","b","c","d","z","w","x","t","q","r","m","n","i","f"};
     
     
    		ArraySort.mergeSort(test1, 14);
     
    		for(int i=0; i < 14; i++)
    			System.out.print(test1[i]+" ");
     
    	}
    }

    and the output
    Exception in thread "main" java.lang.NullPointerException
    	at ArraySort.merge(ArraySort.java:69)
    	at ArraySort.SortMerge(ArraySort.java:51)
    	at ArraySort.SortMerge(ArraySort.java:49)
    	at ArraySort.SortMerge(ArraySort.java:49)
    	at ArraySort.SortMerge(ArraySort.java:49)
    	at ArraySort.mergeSort(ArraySort.java:35)
    	at SortDriver.main(SortDriver.java:15)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Merge Sort

    What line is throwing the error? Something is null on that line. What are you dereferencing?

    Step through your code with a debugger, or at least add some print statements, to figure out what's going on.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Merge Sort

    I think it has something to do with my temp array because once i start trying to put a in to temp that causes the error but i cant create t without setting it to null at first

Similar Threads

  1. Sort and Merge Two Linked List
    By Loraeron in forum What's Wrong With My Code?
    Replies: 49
    Last Post: February 28th, 2013, 10:08 PM
  2. heap sort vs merge sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 3rd, 2012, 04:37 AM
  3. [SOLVED] displaying every 1000 comparisons in merge sort
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 27th, 2012, 07:26 PM
  4. [SOLVED] counting number of comparisons in merge sort
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 26th, 2012, 11:54 PM
  5. Merge sort not sorting at all despite copying it exact from book.
    By javapenguin in forum Other Programming Languages
    Replies: 1
    Last Post: November 1st, 2011, 04:31 PM

Tags for this Thread