java 字符数组冒泡排序

给一个数组 char[] c={'j','a','v','a','1','5'};
要求用冒泡进行排序,并且倒序输出,求解 最好写个方法就行

publicclass BubbleSort{
2publicstaticvoid main(String[] args){
3int score[] = {67, 69, 75, 87, 89, 90, 99, 100};
4for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序
5for(int j = 0 ;j < score.length - i - 1; j++){ //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
6if(score[j] < score[j + 1]){ //把小的值交换到后面
7int temp = score[j];
8 score[j] = score[j + 1];
9 score[j + 1] = temp;
10 }
11 }
12 System.out.print("第" + (i + 1) + "次排序结果:");
13for(int a = 0; a < score.length; a++){
14 System.out.print(score[a] + "\t");
15 }
16 System.out.println("");
17 }
18 System.out.print("最终排序结果:");
19for(int a = 0; a < score.length; a++){
20 System.out.print(score[a] + "\t");
21 }
22 }
23 }
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-12-10
呵呵。就是普通的冒泡排序啊。

char 可以直接转换为 int 的。所以,一样啦。追问

呵呵,我是菜鸟,刚学,可以写出来教教我吗

追答

public static void sort(int []array){
for(int i=1;iarray[j+1]){
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}

或者有现成的。Arrays.sort() 方法.

追问

这个没有倒序啊

追答

你把那个 array[j]>array[j+1]){ 换成小于不就行了。

本回答被提问者和网友采纳
第2个回答  2012-12-11
类似于这样的
============================

public static void main(String[] args) {

char[] c={'j','a','v','a','1','5'};

for(int i=0; i<c.length; i++){
for(int j=0; j<c.length-i-1;j++){
char temp;
if(c[j]<=c[j+1]){
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}
}
}

for(int i=0; i<c.length; i++){
System.out.print(c[i]);
}
}
相似回答