Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games
this week in the
Agile and Other Processes
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Tim Cooke
Sheriffs:
Rob Spoor
Liutauras Vilda
paul wheaton
Saloon Keepers:
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Piet Souris
Bartenders:
Stephan van Hulst
Forum:
Beginning Java
Problem with merge sort
John King
Greenhorn
Posts: 3
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I tried to modify a iterative merge sort class to sort an array of T type generics. It compiles but does not sort properly. Can anyone see what is wrong?
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]; }
Squanch that. And squanch this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
writing generic sort method
Sorting descendingly, but im after ascendingly
Comparing values
Can't Find the bug...
Null pointer Exception
More...