aspose file tools*
The moose likes Beginning Java and the fly likes Problem in Sorting an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in Sorting an Array" Watch "Problem in Sorting an Array" New topic
Author

Problem in Sorting an Array

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Dear friends,
I'm dumbfounded in this particular code snippet, I would like to know your opinion as to how do i fix the problem and also if the below Algorithm is correct.
Please do not post the solution, i dont need it. I want to develop my own solution, all i need is your advice on how do i mend this code.
Lets say there is an integer array with input {-300,1,21,-1,9,10,5}
I want the output as [-300,-1,1,21,10,9,5]
So i wrote the algorithm for it

1. Find the length of the Array and divide it by 2 and deduct 1 from it as array positions start from Zero. Assign this value in a variable called midPosition.
2. Sort the Array in increasing order from Array position Zero to midPosition
3. Sort the Array in decreasing order from midPosition till Array Length
4. Print the Sorted Array

So based on this rough algorithm i wrote the following code


So when i execute the above code, the output is


So as you can see, its not the correct output. Do advise me how do i correct it.
>


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

Your code doesn't compile:

>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 73

Hey,

It looks like your sort for the upper section is slightly off, as it is only picking up 3 values, missing out the 10 and 5 from the original array. I'd suggest going through that bit with a debugger and seeing why it is happening that way.

Also as a style point, you might want to put spaces in the code, it makes it nicer to read. Indentation is fine, but each line seems very scrunched up without spaces.


S


OCAJP 7
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

fred rosenberger wrote:Your code doesn't compile:

>

Strange, why it does not compile for you, for it very well compiles and runs for me because only then i was able to post the output of it too...
Perhaps, you need to check if you copied and pasted it correctly or not!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ashish Dutt wrote:
Strange, why it does not compile for you, for it very well compiles and runs for me because only then i was able to post the output of it too...
Perhaps, you need to check if you copied and pasted it correctly or not!


No, the code displayed in your first post absolutely will not compile. So either the forum's formatting software messed it up, or else the code you provided is not the same code you ran:

This line, for instance, is not legal Java:

>
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Jeff Verdegan wrote:
Ashish Dutt wrote:
This line, for instance, is not legal Java:
>


I am absolutely sure it has something to do with the forum's formatting software.
Let me repost the code again. This reminds me that computers are still noob's, you just cant rely upon them. From this time onwards i would check my code after enclosing it in the code tags, as a precautionary measure.

>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ashish Dutt wrote:This reminds me that computers are still noob's, you just cant rely upon them.


No, you can rely upon them to do exactly what you tell them to do. That won't necessarily bear any resemblance to what you want them to do, however.

And that invalid line is still there. Try putting some whitespace around the tokens.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Thanks winston for the workaround on white-spaces. Hope it fixes the code output
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7838
    
  21

Ashish Dutt wrote:Please do not post the solution, i dont need it. I want to develop my own solution, all i need is your advice on how do i mend this code.

My advice: Don't "mend it", get it right. And if that means re-writing it, then that's what you have to do.

As I see it, you have one (and only one) issue here; but you've lumped it in with a whole bunch of other irrelevant stuff into a great lump which you're now trying to "mend".

And the issue is: How do I sort part of an array without affecting the remainder?

So, how about this:Implement that method and test it thoroughly, so that you know it works for any situation.

Then write your solution.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: Problem in Sorting an Array