冒泡排序

 

package com.duapp.itfanr;

public class ArrayDemo {

public static void bubbleSort(int[] arr) {

for (int x = 0; x < arr.length-1; x++) {
 for (int y =0 ;y<arr.length-1-x;y++){

 if(arr[y]<arr[y+1]){
 int temp = arr[y];
 arr[y] = arr[y+1];
 arr[y+1] = temp; 
 }
 }
 }

}

public static void printArr(int [] arr){
 for (int x = 0; x < arr.length; x++) {
 System.out.println(arr[x]);

}

 }
 public static void main(String args[]) {

int[] arr = { 1, 4, 3, 2, 5 };
 System.out.println("before sort \n") ;
 printArr(arr) ;
 bubbleSort(arr) ;
 System.out.println("after sort \n") ;
 printArr(arr) ;

}

}
 

你可能感兴趣的:(冒泡排序)