排序算法

选择排序

package SelectionSort;

import java.util.Arrays;

/*
    选择排序
 */
public class SelectionSort {
    private SelectionSort(){}

    public static void sort(int[] nums) {
        for (int i=0;i<nums.length;i++) {

            int minIndex = i;
            for (int j=i;j<nums.length;j++) {
                if (nums[j] < nums[minIndex]) {
                    minIndex = j;
                }
            }
            swap(nums,i,minIndex);
        }
    }
    private static void swap(int[] nums,int i,int mindex) {
        int tmp = nums[i];
        nums[i] = nums[mindex];
        nums[mindex] = tmp;
    }

    public static void main(String[] args) {

        int[] nums = {1,4,2,3,6,5};
        sort(nums);
        System.out.println(Arrays.toString(nums));;
    }
}

全部评论

相关推荐

10-14 12:20
门头沟学院 Java
迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务