直接看代码:
package chapter66.hiyo.com;
public class ScoreSortDemo {
public static void main(String[] args) {
int[] enscore = new int[]{3,1,10,43,21,90,6,3,33} ;
ScoreSortDemo sort = new ScoreSortDemo() ;
System.out.println("原始数组:") ;
sort.showArr(enscore);
System.out.println("排序之后的数组:") ;
sort.BubbleSort(enscore);
sort.showArr(enscore);
}
//冒泡排序算法的实现
public void BubbleSort(int[] arr) {
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length-1; j++) {
if(arr[j+1] < arr[j]) {
int temp = arr[j+1] ;
arr[j+1] = arr[j] ;
arr[j] = temp ;
}
}
}
}
public void showArr(int[] arr) {
for(int i : arr ) {
System.out.print(i + " ") ;
}
System.out.println() ;
}
//选择排序算法的实现
public void SelectSort(int[] arr) {
int minIndex ;
for(int i=0; i<arr.length ;i++) {
minIndex = i ;
//寻找最小值的索引
for(int j=i+1; j<arr.length ; j++) {
if(arr[minIndex] > arr[j]) {
minIndex = j ;
}
}
//找到最小值之后,和第一个元素进行交换
int temp = arr[minIndex] ;
arr[minIndex] = arr[i] ;
arr[i] = temp ;
}
}
}