选择排序

来源:互联网 发布:mac os x 10.13 镜像 编辑:程序博客网 时间:2024/05/16 01:09
package lsn1.sjjg.cct.cn.lsn1;import org.junit.Test;/** * 选择排序 * * Created by linyaokui on 17/11/28. */public class SelectSort {    @Test    public void test(){       int[] array = {5,4,2,1,9,8,7,6,3};       selectSort(array);        for(int a : array){            System.out.print(a + " ");        }    }    public void selectSort(int[] array){        for(int i = 0;i<array.length-1;i++){            int index = i; // 选择一个数            for(int j=i+1;j<array.length;j++){                if(array[j] < array[index]){                    index = j;                }            }            if(index != i){ // 表示找到过最小值                int temp = array[index];                array[index] = array[i];                array[i] = temp;            }        }    }}
原创粉丝点击