选择排序

package day;

import java.util.Arrays;

public class Demo3 {
public static void selectionSort(int[] array){
for(int i = 0;i < array.length;i ++){
int min = array[i];
int k = i;
for(int j = i;j < array.length;j ++){
if(min>array[j]){
min = array[j];
k = j;
}
}
swap(array, k, i);
}
}

private static void swap(int[] array, int k, int i) {
int temp = array[k];
array[k] = array[i];
array[i] = temp;
}
public static void main(String[] args) {
int[] a = {-1,2,-3,4,-5,6,-7,8,9};
selectionSort(a);
System.out.println(Arrays.toString(a));
}
}

posted @ 2017-04-27 23:04  起个po名真费劲  阅读(116)  评论(0编辑  收藏  举报