Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Problem in Sorting an Array

Ashish Dutt
Ranch Hand
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.
>

fred rosenberger
lowercase baba
Bartender
Posts: 12085
29

>

Stuie Clarky
Ranch Hand
Posts: 98
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

Ashish Dutt
Ranch Hand
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
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
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
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
Posts: 172
Thanks winston for the workaround on white-spaces. Hope it fixes the code output

Winston Gutkowski
Bartender
Posts: 10103
56
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.