home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Can someone help me with this Java Code?

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1250
Discussions: 200,929, Posts: 2,379,147, Members: 246,296
Old March 30th, 2008, 04:52 AM   Digg it!   #1 (permalink)
Member
 
Join Date: Sep 2004
Location: British Columbia
Posts: 442
Hey guys, my teacher for Computer Science AP told us to merge sort two already sorted arrays combined together.

The array given is:

{1,5,10,15,20,25,3,4,12,21,22}

Array section 1 starts at 0 and ends at 5.
Array section 2 starts at 6 and ends at 10.

basically we are to write a code that will sort the above into:

{1,3,4,5,10,12,15,20,21,22,25}

What I got so far

public int[] merge(int[] list, int a, int b, int c){
//pre: a < b and a and b are within the array bounds
//post: returns a sorted array
int[] temp = new int[ list.length ];

//YOUR CODE TO MERGE
int pos1 = a;
int pos2 = b;
int dest = 0;
for (int i = 0; i< list.length; i++) { //filling temp
temp[i] = list[i];
}
while ( (dest < temp.length) || (pos1 < b) || (pos2 < c) ){ //conditions
if (temp[pos1] < temp[pos2]) {
temp[dest] = temp[pos1];
pos1++;
dest++;
}
else {
int temporary = temp[pos2]; //hold the value at pos2

for (int i = dest; i< pos2; i++) { //shift array one over
temp[i+1] = list[i];
}
temp[dest] = temporary; //put value at pos2
pos2++;
dest++;

for (int h = 0; h < list.length; h++) { //refill list
list[h] = temp[h];
}


}
}
return(temp);
}

The output I'm getting is basically 1 followed by all 3's. I know that somehow the 3 is getting shifted over too. However when I change where the array gets shifted, it still gives me the same printout.

Last edited by RicheemxX : March 30th, 2008 at 04:58 AM.
quantumlight is offline   Reply With Quote
Old March 30th, 2008, 05:50 AM     #2 (permalink)
Member
 
taz480's Avatar
 
Join Date: May 2005
Posts: 355
Send a message via MSN to taz480
Please don't ask people here to do your homework for you.
taz480 is offline   Reply With Quote
Old March 31st, 2008, 09:25 AM     #3 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
taz there's nothing wrong with asking for help. We can't all understand everything all the time. I'll be back later to see if i can help. in class and i just got a text, so now i have to behave...whoops

/edit. Looking at your code, you're not implementing merge sort correctly. Merge sort should be a recursive algorithm unless he just wants you to code one pass

http://en.wikipedia.org/wiki/Merge_sort

you have no comments saying what should be in a, b, and c...fix that!

/edit2. Also, you should wrap your code in the code tags. if you go to the advanced post options, you should see a button with the label "#" Click that and put your code inside of those tags...it helps it stand out.

alternatively you can do

{code} this is my code {/code} - you just have to change the {} to []

it'll look like this

Code:
 this is my code
. This will preserve your formatting (such as indents) and make your code stand out from the rest of your post! Makes it much easier to handle!

I'm going to repost your code properly formatted and such inside the code tags.

Last edited by sr71000 : March 31st, 2008 at 10:15 AM.
sr71000 is offline   Reply With Quote
Old March 31st, 2008, 10:37 AM     #4 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
Code:
/* merge(int[] list of ints to be sorted, int ?, int ?, int ?)
  *
  * PRE/POST CONDITIONS
  * pre: a < b  and a and b are within the array bounds
  * post: returns a sorted array
  */
public int[] merge(int[] list, int a, int b, int c)
{
	//Declare Variables
	int[] temp = new int[ list.length ];
	int pos1 = a;
	int pos2 = b;
	int dest = 0;

	//filling temp - why?  Why not just pull values straight from list as you go?
	for (int i = 0; i< list.length; i++)
		temp[i] = list[i];

	while ( (dest < temp.length) || (pos1 < b) || (pos2 < c) )
	{
		if (temp[pos1] < temp[pos2])
		 {                                           
			temp[dest] = temp[pos1];
			pos1++;
			dest++;
		}
		else 
		{
			//hold the value at pos2
			int temporary = temp[pos2];   
                      
			//shift array one over
			/////////////////////////////////////////
			//HERE IS YOUR MAJOR ISSUE!!!!!
			/////////////////////////////////////////
			//Remember when you do this, you're shifting the remainder of your array to the right 1 space.  That means you're moving position 1 and two as well as the beginning and the end of both parts of your array! (meaning a, b, and c)
			//Since you may also be changing a, b, and c we need to know exactly what they are!  WE NEED MORE INFO ABOUT A, B, AND C!!!  I'm only being a pita about this because this may be a simple problem but if you're going to ask for help you need to learn to properly document, especially with your input variables!
			/////////////////////////////////////////
			for (int i = dest; i< pos2; i++)
				temp[i+1] = list[i];
			temp[dest] = temporary;                               //put value at pos2
			pos2++;
			dest++;
			for (int h = 0; h < list.length; h++)                //refill list
				list[h] = temp[h];
		}
	}
	return(temp);
}
I know what a, b, and c do, but i'm not helping any more till I see proper documentation except to say you should rethink your design entirely. You're doing SO much more work than you have to. Rethink this without filling temp first and then trying to sort them in place (in the array). There's a much easier way to do this!

-Kevin
sr71000 is offline   Reply With Quote
Old November 9th, 2008, 04:02 PM     #5 (permalink)
Junior Member
 
Join Date: Nov 2008
Posts: 4
merge two sorted arrays in C++

void merge_sort(apvector <int> &arrayA, apvector <int> &arrayB, apvector <int> &arrayC)
{
int indexA = 0; // initialize the Array Indices
int indexB = 0;
int indexC = 0;

while((indexA < arrayA.length( )) && (indexB < arrayB.length( ))
{
if (arrayA[indexA] < arrayB[indexB])
{
arrayC[indexC] = arrayA[indexA];
indexA++; //increase the index
}
else
{
arrayC[indexC] = arrayB[indexB];
indexB++; //increase the index
}
indexC++; //move to the next position in the new array
}
// Push remaining elements to end of new array when 1 feeder array is empty
while (indexA < arrayA.length( ))
{
arrayC[indexC] = arrayA[indexA];
indexA++;
indexC++;
}
while (indexB < arrayB.length( ))
{
arrayC[indexC] = arrayB[indexB];
indexB++;
indexC++;
}
return;
}


/************************************************** ************/
*this checks which item is smaller and puts it in array C while there are still items,
* when one stack runs out it adds the rest of the items to the end of Arrray C.
* enjoy
***************************************/
jd123 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
The Java Code Thread xelnanga Webmastering and Programming 11 December 10th, 2007 09:46 PM
Launching Java webpages with different Java settings? LostBok Webmastering and Programming 1 December 11th, 2006 03:38 AM
Need help with some Java Code :P Soheils91 General Tech Discussion 2 October 20th, 2004 08:57 PM
comple java code!?!? gnu? zskillz Webmastering and Programming 4 October 28th, 2003 03:09 PM
silly java code problem jgargac Webmastering and Programming 10 March 2nd, 2002 04:11 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2868)
Obama the Muslim (14)
California Passes Anti-Flat-HDTV Le.. (39)
Is the PSU I received dead? (10)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Foreign voltage (10)
Install XP pro and a Vista laptop ?.. (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Ideal cheap graph card for PC-Gamin.. (16)
New Computer wont recognize XP disc (7)
EVGA 9800 gtx help with finding a g.. (8)
Recent Discussions
Regular Build (3)
solutions for virtical white lines on.. (0)
Ideal cheap graph card for PC-Gaming? (16)
Graphics Card Upgrade Question (0)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Install XP pro and a Vista laptop ?? (10)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)
Browsers wont load websites (2)
Virus Doctor Popup? (1)
Dept. of HS: NSA 'Helped' Develop Vis.. (15)


All times are GMT -4. The time now is 04:02 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28