• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting string with out using predefined java methods

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i am trying to sort some strings with out using predefined methods in java
but it is not working properly

can any one gind me the bugs in this program???
:roll: :roll:
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your compare method is supposed to return a positive answer if a > b, zero if the two are the same, and a negative answer if a < b.
I can see no way to get a negative answer out of your compare method.

BTW. You will find it quicker to use arithmetic on the char values: you can calculate char1 - char2.

You are using l (don't use small L; it is too easy to confuse with the number 1) as the count of comparisons. But that will give a zero result if you compare the Strings "Camp" and "Campbell."

Initialise your loop counter in the for statement.
Initialise your temp value in the if block.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested your application and came to following conclusion....

if leftChar<rightChar then set r=-1;
if leftChar==rightChar then set r=0;
if leftChar>rightChar then set r=+1;

With these changes and slightly I changed inner for loop,

here is the code...




Naseem
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do one more thing, before passing it to myStrCmp(String a,String b), call trim() otherwise you will get wrong result.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic