This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "problem in simple bubble sort logic.." Watch "problem in simple bubble sort logic.." New topic
Author

problem in simple bubble sort logic..

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
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
Rancher

Joined: Aug 22, 2006
Posts: 257

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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: problem in simple bubble sort logic..
     
    Similar Threads
    why does core java doesnt have unsigned int??
    not able to call my header file
    Largest/Greatest of n numbers
    C2143 error and I can't find it
    C to Java Translation, HELP!