Merge sort problem.

Thread Solved
Reply

Join Date: Sep 2007
Posts: 36
Reputation: DeadJustice is an unknown quantity at this point 
Solved Threads: 0
DeadJustice DeadJustice is offline Offline
Light Poster

Merge sort problem.

 
0
  #1
Nov 22nd, 2008
I tried to modify this merge sort method to sort an array of type T. Its an merge sort except iterative. I was just wondering why it wouldn't sort right? Can anyone help me out?

  1. public static <T extends Comparable<? super T>> void iterativeMergeSort(T[] input)
  2. {
  3. // Create a temporary array for sorting
  4. Class elementType = input.getClass().getComponentType();
  5. T temp[] = (T[]) java.lang.reflect.Array.newInstance( elementType, input.length);
  6.  
  7. // Initialize variable to store size of segments
  8. int size = 0;
  9. mergePass(input, temp, size, input.length /2);
  10. mergePass(input, temp, input.length /2, input.length -1);
  11. }
  12.  
  13. public static <T extends Comparable<? super T>> void mergePass(T[] input, T[] temp, int size, int end)
  14. {
  15. // Initialize index of next segment
  16. int indexNext = size;
  17. while (indexNext < end)
  18. {
  19. //need to divide the array into two parts
  20. merge(input, size, (indexNext + size + 1)/2 - 1, indexNext + 1,temp);
  21. indexNext = indexNext+1;
  22. }
  23. for (int copy = size; copy < end; copy++)
  24. {
  25. temp[copy] = input [copy];
  26. }
  27. }
  28.  
  29. private static <T extends Comparable<? super T>> void merge (T[] a, int begin, int mid, int end, T[] tmp)
  30. {
  31. int i = begin; // keeps track of where I am in first half
  32. int j = mid + 1; // keeps track of where I am in second half
  33. int k = 0; // keeps track of where I am in merged list
  34.  
  35. while (i<=mid && j<=end) { // go while both halves have elements
  36. if (a[i].compareTo(a[j]) < 0)
  37. tmp[k++] = a[i++];
  38. else
  39. tmp[k++] = a[j++];
  40. }
  41.  
  42. // copy the tail of the half that still has elements left
  43. // only one of these two while loops will execute, b/c either i>mid or j>end
  44. while ( j <= end )
  45. tmp[k++] = a[j++];
  46. while ( i <= mid )
  47. tmp[k++] = a[i++];
  48.  
  49. // Copy tmp, which is sorted, back to a[begin..end]
  50. for ( k = 0, i = begin; i <= end; k++, i++)
  51. a[i] = tmp [k];
  52. }
Last edited by DeadJustice; Nov 22nd, 2008 at 3:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 328
Reputation: quuba is on a distinguished road 
Solved Threads: 51
quuba quuba is offline Offline
Posting Whiz

Re: Merge sort problem.

 
1
  #2
Nov 22nd, 2008
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: DeadJustice is an unknown quantity at this point 
Solved Threads: 0
DeadJustice DeadJustice is offline Offline
Light Poster

Re: Merge sort problem.

 
0
  #3
Nov 22nd, 2008
Thanks, I see what I did wrong and I got it working now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC