- package com.oracle.demo01;
- import java.util.Arrays;
- // 冒泡排序, 两两比较, 一次得一个最大值
- public class Demo10 {
- public static void main(String[] args) {
- int[] arr={1,2,3,6,5,478,96,12};
- int temp=0;
- for(int i=0;i<arr.length-1;i++){// 因为最后一个不用再进行比较
- for(int j=0;j<arr.length-1-i;j++){
- if(arr[j]>arr[j+1]){
- temp=arr[j];
- arr[j]=arr[j+1];
- arr[j+1]=temp;
- }
- }
- }
- System.out.println(Arrays.toString(arr));
- }
- }
来源: http://www.bubuko.com/infodetail-3351880.html