• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in Sorting an Array

 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
>
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code doesn't compile:

>
 
Ranch Hand
Posts: 104
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks winston for the workaround on white-spaces. Hope it fixes the code output
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic