• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

simple sorting algorithm

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I got this little code which should actually sort the numbers in an array from the smallest to the highest,



At least I dont get any error messages (yeah!) but the result ist not sorted.

It must have to do with the two for loops (is this the right term for it in English?)

I hope you guys could give me a hint

Greetings from Germany

Max
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run your code there are only two values out of place: 3 and 4. The 3 makes sense; you start both i and j at 1, thereby always ignoring location 0. If I let both start at 0 then only 4 is out of place. That is not coincidentally the last element when you start.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

The logic is not correct.

PLease find the bellow steps
1. For loop from 0 to end of size of loop(u should start with 0)
2. 2nd for loop that should start with the i+1 to end of loop.
3. them make the comparison between the elements of two loops.

I believe its not clear..

Look for each element in first loop you need to compare the elements of the array(thats what is the 2nd loop for) and then select the least element(by giving 'if' condition in the 2nd loop). Now you need set the least element in the 1st position and the 1st position element in the place of least element. When the two loops completes you get the sorted array.

Try to implement this. It will work.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic