冒泡排序
2018-02-02 本文已影响0人
赖熊
1.冒泡排序
package com.cts.elt.sort;
import java.util.Arrays;
public class MaopaoSort {
public int[] arr;
public void sort(){
int in;
int out;
for (out =arr.length-1;out>1;out--){
for (in=0;in<out;in++){
if (arr[in]>arr[in+1]){
swap(in,in+1);
}
}
}
}
public void swap(int a,int b){
int t =arr[a];
arr[a]=arr[b];
arr[b]=t;
}
public static void main(String [] args){
MaopaoSort maopaoSort =new MaopaoSort();
maopaoSort.arr =new int[]{1,8,4,7,2};
maopaoSort.sort();
System.out.println(Arrays.toString(maopaoSort.arr));
}
}