| | |
Merge sort problem.
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 36
Reputation:
Solved Threads: 0
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?
Java Syntax (Toggle Plain Text)
public static <T extends Comparable<? super T>> void iterativeMergeSort(T[] input) { // Create a temporary array for sorting Class elementType = input.getClass().getComponentType(); T temp[] = (T[]) java.lang.reflect.Array.newInstance( elementType, input.length); // Initialize variable to store size of segments int size = 0; mergePass(input, temp, size, input.length /2); mergePass(input, temp, input.length /2, input.length -1); } public static <T extends Comparable<? super T>> void mergePass(T[] input, T[] temp, int size, int end) { // Initialize index of next segment int indexNext = size; while (indexNext < end) { //need to divide the array into two parts merge(input, size, (indexNext + size + 1)/2 - 1, indexNext + 1,temp); indexNext = indexNext+1; } for (int copy = size; copy < end; copy++) { temp[copy] = input [copy]; } } private static <T extends Comparable<? super T>> void merge (T[] a, int begin, int mid, int end, T[] tmp) { int i = begin; // keeps track of where I am in first half int j = mid + 1; // keeps track of where I am in second half int k = 0; // keeps track of where I am in merged list while (i<=mid && j<=end) { // go while both halves have elements if (a[i].compareTo(a[j]) < 0) tmp[k++] = a[i++]; else tmp[k++] = a[j++]; } // copy the tail of the half that still has elements left // only one of these two while loops will execute, b/c either i>mid or j>end while ( j <= end ) tmp[k++] = a[j++]; while ( i <= mid ) tmp[k++] = a[i++]; // Copy tmp, which is sorted, back to a[begin..end] for ( k = 0, i = begin; i <= end; k++, i++) a[i] = tmp [k]; }
Last edited by DeadJustice; Nov 22nd, 2008 at 3:50 pm.
![]() |
Similar Threads
- merge sort not working (C++)
- merge sort (C++)
- Merge sort not working (C++)
- Merge Sort Code Not Working Properly In Vc++.code Is Given (C++)
- Insertion Sort Problem (C++)
- strstream problem (C++)
- hw assignment help on selection & merge sort (C)
- Link List Sorting Problem (C++)
- another merge sort question (C++)
Other Threads in the Java Forum
- Previous Thread: countdown timer program
- Next Thread: Passing Array to Method
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows





