用泛型编写一个冒泡排序法(java语言)
- 提问者网友:沉默菋噵
- 2021-05-10 06:39
- 五星知识达人网友:七十二街
- 2021-05-10 06:48
public static void main( String[] args ) {
Integer[] a = {3,6,5,4,1,2,9,8,7,5,6,2};
Double[] b = {3.2,6.6,5.0,1.1,9.3,5.2,4.2,8.3,7.8,5.32,6.1};
sort( a );
sort( b );
for ( Integer i : a )
System.out.print( i + " " );
System.out.println();
for ( Double i : b )
System.out.print( i + " " );
}
static <T> void sort( T[] a ) {
T t;
for ( int i = 0; i < a.length; ++i )
for ( int j = a.length - 1; j > i; --j ) {
if ( ((Comparable)a[j]).compareTo(a[j-1]) < 0 ) {
t = a[j];
a[j] = a[j-1];
a[j-1] = t;
}
}
}
}
- 1楼网友:慢性怪人
- 2021-05-10 07:13
public class Test1 { public static void main(String[] args) { Integer[] elems = { 2, 3, 4, 5, 1 }; new BubbleSort<Integer>().sort(elems);
} }
class BubbleSort<E extends Comparable<E>> {
public void sort(E... elems) { // TODO Auto-generated method stub E temp; int count = 1; for (int k = 0; k < elems.length; k++) { System.err.print(elems[k] + " "); } System.err.println(); System.err.println("---------------------"); for (int i = 0; i < elems.length; i++) { for (int j = elems.length - 1; j > i; j--) { if (elems[j].compareTo(elems[j - 1]) < 0) { temp = elems[j - 1]; elems[j - 1] = elems[j]; elems[j] = temp; } System.err.print("count-" + count + ":"); for (int k = 0; k < elems.length; k++) { System.err.print(elems[k] + " "); } System.err.println(); count++; } System.err.print("count-" + count + ":"); for (int k = 0; k < elems.length; k++) { System.err.print(elems[k] + " "); } System.err.println(); count++; } System.err.println("---------------------"); for (int k = 0; k < elems.length; k++) { System.err.print(elems[k] + " "); } System.err.println(); } }
糯米饭的盐什么时候放啊? |
谁有韦弗的最新消息 |
夜店是什么吖? |
兰州什么地方能看双子座流星雨??还有时间和 |
激浪兑奖网页打不开 |
为什么电脑突然卡死? |
炫舞可以打包给我吗? |
深圳龙华广发证券在哪里 |
男人怎样才能收拾的精神点 |
和自己爱的人分手了怎么办? |
初二数学题(急) |
怎么样才能每天都很开心 |
网队的战计 |
中小学生比赛职中生能参加吗? |
16周岁可以在福州永辉超市工作吗? |