选择排序

来源:互联网 发布:2016淘宝新店如何运营 编辑:程序博客网 时间:2024/05/18 02:12
package test;


public class SelectSort {
/*
* 直接选择排序(也是两个循环结构) 只需要找出最小值的左边
*/
public static void main(String[] args) {
int arr[] = { 9, 4, 3, 1, 8, 2, 5, 6 };
sort(arr);
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k] + " ");
}
}


public static void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;//定义一个最小值


for (int j = i + 1; j < arr.length - 1; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}


}
0 0
原创粉丝点击