祁连山南路1988号:用JAVA写出冒泡排序的算法

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/06 14:46:25

我给你写一个完整的算法:
Public Class Bubblesort{
Public static void main(string args[]){
int array[] = {"55","44","22","14","5"};
for(int i = 0;i<array.length;i++){
System.out.print(array[i] + ",");
}
Bubblesort b = new Bubblesort();
int[] result = b.bubble(array);//调用函数
for(int i = 0;i<result.length;i++){
System.out.print(result[i] + ",");
}
int[] bubble(int a[]){ //函数
int temp;
int size = a.length;
for(int i = size - 1;i>=1;i--){
for(int j = 0;j<i;j++){
if (a[j]>a[j+1]){ 此处可以调整升序与降序
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
}
}

给你两个,一个冒泡,一个选择

public void bubbleSort(int a[]) { //数组的冒泡排序
int n = a.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

public void selectSort(int a[]) { //数组的选择排序
for (int n = a.length; n > 1; n--) {
int i = max(a, n);
int temp = a[i];
a[i] = a[n - 1];
a[n - 1] = temp;
}
}