Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Program to sort the characters in the string  RSS feed

 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please help me out the java program to sort the characters for the given string without using sort() method?
For example, if we are giving the input as "sai",the output should be "ais".
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. What ideas and/or code do you have so far?
 
Oggi Olli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a vast amount of different sorting algorithms, bubblesort being the easiest and slowest, but works fine in a developing context.. Otherwise you've got selection sort, quicksort and heapsort to mention a few.

Which one do you want to use?
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd just get the char[] and use Arrays.sort() on that one.

Seeing as you can't or don't want to use that approach I think this is a homework assignment, and you have to write your own sorting code.

Check out Wikipedia and Google, there are enough examples to be found.
 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know the logic behind sort() method.So that I asked this program without using sort().
Could you please tell me this progm using bubble sort method
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This explains the algorithm that a bubble sort uses. Try implementing it in Java and if you have any problems, show us your code.
 
Oggi Olli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Arrays.sort() method is overloaded and use different sorting algorithms depending on what type of array it is supposed to sort.

If you read the source file (Arrays.class) you will see many examples of different sorting algorithms.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that Arrays.sort() always uses the same algorithm - it just has multiple implementations for different data types. It's a good algortihm, certainly, much faster than a bubble sort. But for a beginner it may well be easier to understand the bubble sort.
 
Oggi Olli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I would say that Arrays.sort() always uses the same algorithm - it just has multiple implementations for different data types. It's a good algortihm, certainly, much faster than a bubble sort. But for a beginner it may well be easier to understand the bubble sort.


Well, sort of..
I found Insertion sort (on smallest arrays), Mergesort (on Object arrays) and quicksort elsewhere.
[ October 23, 2007: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the edit above - I accidentally hit edit rather than reply, but I was able to restore the original text.

Hm, OK. I would argue that using insertion sort for small arrays isn't based on the type of the array and overloading. But I didn't realize that both quicksort and heapsort were in there; thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!