Results 1 to 1 of 1
Thread: Merge Sorting
- 03-23-2011, 06:54 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Merge Sorting
Can anyone please help me with this? Appreciate it a lot.
I am to code a merge method for sorting an array of n size which uses another array of b of size n/2. Here is what I have so for:
My main method has:
int[] random ={1,7,8,10,2,4,5,6};
mergeHalf(random,new int [random.length/2 + random.length%2],0,3,7);
This should work for n= 32 etc. In this example, random is sorted from index 0-3 and 4-7, that is why method call is 0,3,7. Tried to figure it out with this example, but I have a sort method as well that will sort the 2 halves of array and then call the mergeHalf method.
b is initially an empty size of n/2 ( 4 elements with this example of random 8 elements) and is used as a temp array . For now, it only sorts the first 4 elements of random.
Thanks a lot
public static void mergeHalf(int[] a, int[] b, int i, int j, int k) {
int leftEnd = j;
int tmpPos = i;
// int numElements = k - i + 1;
int jCount=0;//number of elements removed from 2nd part of a
while (i <= leftEnd && j + 1 <= k) {
if (tmpPos >= b.length){
printArray("A", a);
//shift
for(int s = i; s<=leftEnd; s++){
a[s+jCount]=a[s];
}
//j+= jCount;
i+= jCount;
//assign A from B
for (int i1=0; i1 < b.length; i1++){
a[i1] = b[i1];
}
jCount=0;
tmpPos=0;
leftEnd= j;
printArray("A", a);
printArray("B", b);
//System.exit(0);
}
if (a[i] <= a[j + 1])
b[tmpPos++] = a[i++];
else{
b[tmpPos++] = a[j++ +1];
jCount++;
}
}
Similar Threads
-
Merge Two Xml files ????
By alwz_nikhil in forum XMLReplies: 5Last Post: 01-18-2011, 09:18 AM -
Merge XML using XSL
By palanikumark in forum XMLReplies: 1Last Post: 08-16-2008, 12:14 AM -
How do merge two xml files into one xml?
By veera in forum XMLReplies: 1Last Post: 03-27-2008, 05:06 PM -
Merge 2 button become one
By banie in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 05:26 PM -
Merge Sort Help
By Hollywood in forum New To JavaReplies: 5Last Post: 01-30-2008, 03:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks