我想对字符串“computer”进行排序 - > “cemoprtu”,但不使用Arrays.sort(string)。
答案 0 :(得分:2)
看起来你需要对字符进行排序,所以我从
开始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
答案 1 :(得分:1)
查看不同的sorting algorithms并实施一对,尝试冒泡然后快速排序。
答案 2 :(得分: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);
}
}
}
答案 3 :(得分: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 = 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]);
}
}
}