package cn.niit.text;
import java.util.Arrays;
/*
* 冒泡排序
*/
public class Demo2 {
public static void BubbleSort(int[] arr) {
int temp;//定义一个临时变量
for(int i=0;i<arr.length-1;i++){//冒泡趟数
for(int j=0;j<arr.length-i-1;j++){//每趟循环次数
if(arr[j+1]<arr[j]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] = new int[]{8,6,3,9,5};
Demo2.BubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}
import java.util.Arrays;
/*
* 冒泡排序
*/
public class Demo2 {
public static void BubbleSort(int[] arr) {
int temp;//定义一个临时变量
for(int i=0;i<arr.length-1;i++){//冒泡趟数
for(int j=0;j<arr.length-i-1;j++){//每趟循环次数
if(arr[j+1]<arr[j]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] = new int[]{8,6,3,9,5};
Demo2.BubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}