java 冒泡排序


import java.util.*;


public class Bubble {

private ArrayList<Integer>array;

public Bubble() {

// TODO Auto-generated constructor stub

this.array =new ArrayList<Integer>();

}

public ArrayList<Integer> bubble(ArrayList<Integer>arr){

ArrayList<Integer> array = arr;

if(array==null){

System.out.println("排序数组不存在");

}else if(array.size()==1){

System.out.println(array.get(0));

}

        for(inti=0;i<array.size();i++){

        for(intj=0;j<array.size()-1;j++){

        if(array.get(j)>array.get(j+1)){

        intt = array.get(j);

        array.set(j,array.get(j+1));

        array.set(j+1,t);

        }

        }

        }

        return array;

}


///输入数组元素,输入N结束输入

public void inputItem(){

    System.out.println("请依次输入数组数值,每次输入一个,回车录入数值,输入N结束输入进行排序");

Scanner sc =new Scanner(System.in);

        booleancanContinueInput = true;

while(canContinueInput){

String strInput =sc.nextLine(); 

if(strInput.equals("N")){

canContinueInput =false;

}else{

try{

int num = Integer.parseInt(strInput);

this.array.add(num);

// System.out.println("输入为:"+num);

}catch(Exceptione){

System.out.println("输入有误");

}

}

}

System.out.println("待排序数组:"+this.array);

}

public ArrayList<Integer> getArray() {

returnarray;

}


public void setArray(ArrayList<Integer> array) {

this.array =array;

}


public static void main(String[] args) {

// TODO Auto-generated method stub

Bubble bubble =new Bubble();

bubble.inputItem();

ArrayList<Integer> result = bubble.bubble(bubble.getArray());

bubble.setArray(result);

        System.out.println("排序结果:"+result);

//        System.out.println("排序结果:"+bubble.getArray());

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值