Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in simple bubble sort logic..

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic