This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes C / C++ and the fly likes problem in simple bubble sort logic.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "problem in simple bubble sort logic.." Watch "problem in simple bubble sort logic.." New topic

problem in simple bubble sort logic..

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1012
hello i m just trying to do searching and sortings at my own.
i don't know but i am doing something wrong in my bubble sort code.
can anyone help me in this, below is my code:

there is something wrong in my sort function.
my data is not getting sort.
i also search on google, i found lots off code there, but i want to know what i am doing wrong..

Thank you...

Anand Hariharan

Joined: Aug 22, 2006
Posts: 272

I haven't looked at it in great detail, but usually, when I see <= as the condition in a for loop, methinks it is wrong. For example, it appears that your inner (i.e., j index for loop) when i = n-1 and j=i will access array out of bounds.

Some other points to consider:

  • Terminate your printf string literals with \n (new line) character
  • Always check scanf's return value.
  • Try to avoid I/O altogether (nothing wrong in hard-coding your inputs within your source when you are simply trying to learn sort algorithms).

  • best wishes,
    - Anand

    "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
    Don't get me started about those stupid light bulbs.
    subject: problem in simple bubble sort logic..
    It's not a secret anymore!