栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

选择排序.

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

选择排序.

选择排序
public class SelectSort {
    public static void main(String[] args) {
        int[] a = {2, 6, 3, 8, 4, 9, 7};
        int len = a.length;
        for (int i = 0; i < len-1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < len; j++) {
                //拿到单次循环里的最小值
                minIndex = a[j] < a[minIndex] ? j : minIndex;
            }
            //把最小值放到第一位
            swap(a, i, minIndex);
        }
        System.out.println(Arrays.toString(a));

        String str1 = new StringBuilder("计算机").append("软件").toString();
        String str6 = new StringBuilder("计算机").append("软件").toString();
        String str7 = str6.intern();
        String str2 = str1.intern();
        String str5 = "计算机软件";
        String str3 = new StringBuilder("ja").append("va").toString();
        String str4 = str3.intern();
        System.out.println(str2==str5); //t
        System.out.println(str1==str2); //f
        System.out.println(str3==str4); //f
        System.out.println(str2==str6); //t
        System.out.println(str1==str6); //f
        System.out.println(str2==str7); //t
    }

    private static void swap(int[] a, int i, int minIndex) {
        //把i 和 minIndex 交换
//        a[i] = a[i] ^ a[minIndex];   //错误的,前提是不能是一块地址 这里的i和minIndex是一个
//        a[minIndex] = a[i] ^ a[minIndex];  //a[i]
//        a[i] = a[i] ^ a[minIndex];  //a[minIndex]
        int tmp = a[i];
        a[i] = a[minIndex];
        a[minIndex] = tmp;
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573121.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号