0

I want to sort a string "computer" -> "cemoprtu", but without using Arrays.sort(string).

1
  • 2
    Has any particular sorting algorithm been given to you to implement? Commented Mar 20, 2011 at 8:32

6 Answers 6

2

Looks like you need to sort the characters, so I'd start with

String input = "computer";
char[] characters = input.toCharArray();
//now sort characters using some algorithm
String output = new String(sorted_characters); //sorted_characters might be characters after sorting, if you sort in place
Sign up to request clarification or add additional context in comments.

Comments

1

Check out different sorting algorithms and implement a couple, try bubble then quick sort.

1 Comment

Every time I see bubble sort I die a little on the inside. (Just my opinion)
1
package practice;


class Practice
{
public static void main(String args[])
{
   String s = "bcfaed";
   char a[]=s.toCharArray();
   char b[]=new char[a.length];
   int count=0;
   for(int i=0;i<a.length;i++)
   {
       count=0;
       for(int j=0;j<a.length;j++)
       {
       if(a[i]<a[j])
       {
       count++;
       }
       }
       b[count]=a[i];
   }
   for(char x:b)
   {
       System.out.println(x);
   }
}
}

Comments

1
class Abc {

    public static void main(String[] args) {
        String str = "welcome";
        char temp = 0;

        char arr[] = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (arr[j] > arr[i]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}

Comments

0
class Abc {

    public static void main(String[] args) {
        String str = "welcome";
        char temp = 0;

        char arr[] = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] > arr[i]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}

Comments

0
// Java program to sort 
// a string of characters 
public class SortString{ 
    static final int MAX_CHAR = 26; 
  
    // function to print string in sorted order 
    static void sortString(String str) { 
  
        // Hash array to keep count of characters. 
        int letters[] = new int[MAX_CHAR]; 
  
        // Traverse string and increment 
        // count of characters 
        for (char x : str.toCharArray()) { 
  
            // 'a'-'a' will be 0, 'b'-'a' will be 1, 
            // so for location of character in count 
            // array we will do str[i]-'a'. 
            letters[x - 'a']++; 
        } 
  
        // Traverse the hash array and print 
        // characters 
        for (int i = 0; i < MAX_CHAR; i++) { 
            for (int j = 0; j < letters[i]; j++) { 
                System.out.print((char) (i + 'a')); 
            } 
        } 
    } 
  
    // Driver program to test above function 
    public static void main(String[] args) { 
        sortString("eeffjaaannnfffnn"); 
    } 
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.