This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Please help in this selection sort  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm doing a program which is Selection Sort of an array of String... I want to get a 20 length string consist of letters from a-z and A-Z randomly...
I used String Buffer to get this random string but I'm having problems in doing the selection sort...
This is my code:

Any help is greatly appreciated...
 
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the bit about < 0 mean? That looks possibly incorrect to me.
you should lose the te variable and replace it with array.length.
You should move as much code out of the main() method as possible.
 
Campbell Ritchie
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Yazan Rashi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used this loop in selection sort with String array and without using StringBuffer... and it worked fine with me...

The difference now is that I'm trying to generate Random String using StringBuffer... and it's something new to me Also, I don't know too much about Char arrays...
Help me please!
 
Campbell Ritchie
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have posted now is different. You have added compareTo now, which won't work on a char.

Do a search for arithmetic on chars. You will find this, which may help with your chars.
 
Yazan Rashi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this code I used it with String Array, not Char Array...
Thanks a lot for your replies... I really appreciate your help...
 
Campbell Ritchie
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Char array? Do you mean a char array? The two are different.
You will have to use different techniques for comparing chars from Strings; a reference type and a primitive behave differently.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd ask exactly what kind of "problems" are you having. Compile time? Runtime? Code throws an exception or doesn't run correctly?

A little more detail would help, because the code you posted here won't even compile for me.

Also confusing for me is what you're using the StringBuffer for. You load it with a random set of characters from array, but then you don't use it again.

You are performing operations on array, and those won't affect sb at all, even if they would work. Rethink your logic, then update your code accordingly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!