package com.fuxi;
import java.util.Random;
public class Sort {
public static void main(String[] args) {
Random random = new Random();
int[] arr = new int[15];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(50)+1;
}
System.out.print("排序前:");
printArr(arr);
System.out.println();
System.out.print("选择排序后:");
select(arr);
System.out.println();
System.out.print("冒泡排序后:");
maoPaoSort(arr);
System.out.println();
}
public static void select(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]){
jiaohuan(arr,i,j);
}
}
}
printArr(arr);
}
public static void maoPaoSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if(arr[j] > arr[j+1]){
jiaohuan(arr,j,j+1);
}
}
}
printArr(arr);
}
public static void jiaohuan(int[] arr,int i,int j){
int a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
public static void printArr(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" \t");
}
}
}
比较重要吧,老是记不住,放在博客上