初学java 用冒泡算法写了一个bubble_sortde类完成冒泡算法
package algorithm;
public class bubble_sort {
private static int i=0;
private static int j=1;
private static double n=0;
private double[] ant={0,0,0,0,0,0} ; //存储器:java没有动态数组初始化成这样我也很无奈
boolean gorbreak(double[] arry1,double[] arry2){//判断存储器中每一次是否与上一次变化不相等从而判断是否迭代结束
int temp=0;
for(int d=0;d<arry1.length;d++){
if (arry1[d]!=arry2[d])temp=1;
}
if(temp==1){
return true;
}else {
return false;
}
}
public void bubble_one(double[] p){ //一次排序过程
if(j==p.length){
i=0;j=1;
}else if(p[i]>p[j]){
n=p[j];
p[j]=p[i];
p[i]=n;
i++;
j++;
bubble_one(p);
}else if(p[i]<=p[j]){
i++;
j++;
bubble_one(p);
}
}
public void bubble_complete(double[] cp) {//完全排序过程
while (gorbreak(cp,ant)){
for(int k=0;k<cp.length;k++){
ant[k]=cp[k];
}
bubble_one(cp);
}
}
public void display(double[] cp){// 显示经过算法后后的结果
for(int arymbr=0;arymbr<cp.length;arymbr++){
System.out.println(cp[arymbr]);
}
}
}
然后就是弄一个text试试看
package algorithm;
public class text {
public static void main(String[] args) {
double[] array1={12,23,12,22,43,1};
bubble_sort text1=new bubble_sort();
text1.bubble_complete(array1);
text1.display(array1);
}
}
好的就是这样