• Post Reply Bookmark Topic Watch Topic
  • New Topic

hello.Bubblesort  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this makes sort my array? how would i express this code to string .If example i was searching a string.Thanks
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote: this makes sort my array? how would i express this code to string .If example i was searching a string.Thanks



like this?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Strings,
if (coden[j] > coden[j+1])
doesn't make sense. You need to use String's compareTo method.

And the inner loop of your code is incorrect - it can address coden[j+1] when j=coden.length-1 which will invoke an IndexOutOfRange Exception.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:
Fred Kleinschmidt wrote:For Strings,
if (coden[j] > coden[j+1])
doesn't make sense. You need to use String's compareTo method.

And the inner loop of your code is incorrect - it can address coden[j+1] when j=coden.length-1 which will invoke an IndexOutOfRange Exception.
is right now?
i dont know why is false i post it please can you tell me?second i want to do this method
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code currently is not readable. Please indent and format it, so someone could read it and finallly help you.

Nice indentation example:
  • Once you declare a class, hit return.
  • If you have some instance fields, declare them right below the class declaration. These should be shifted by 4 spaces to the right (look at the tip at the very bottom how to do that).
  • If no instance fields, leave an empty line, and only then start method declaration. Use the same tip (extra: look to an example above).
  • Every and each declared method suppose to be separated by an empty line. Look to main and bubbleSort methods.
  • Code which operates on different logic parts should be separated by an empty line too. Look at code example lines 9-13 vs 15-17.
  • Make sure you use curly braces which defines method or loop or if statement body consistently (choose one of the two options and never omit them):

  • Tip: Each level shifting to the right suppose to be by 4 spaces. You can do that by hitting space keystroke 4 times. Don't use TAB keystroke, unless you have set up your programming environment properly, so these would be treated as 4 singular spaces - otherwise it messes up indentation per each platform.

    Start solving your exercise by fixing your indentation and formatting as described above.
     
    ekte spiriopoulos
    Ranch Hand
    Posts: 217
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I want bubblesort for sort double or string . What changes should I do?
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I already told you to look at String's compareTo() method. that is, instead of "a < b" you need "a.compareTo(b) < 0"
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You posted code multiple times and methods signature differs in almost all code snippets.
    Concentrate on particular one first, get that working, then move on towards another.

    BUT before that:
  • You need to learn how to indent and format your code
  • Plus, you need to learn other coding conventions, that variable names starts with a lower case (unless it is constant)
  • Method names starts with a lower case too
  • Class names starts with an upper case
  • Such variables as F should be avoided in pretty much all situations, unless you working with Fahrenheit

  • There are more things to learn, but try to implement those first.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!