排序算法举例

本人面试过京东、阿里、拼多多、字节跳动等一系列大厂,和大家分享一下常见排序算法。

数组快速排序

复制代码
import java.util.Arrays;
import java.util.Random;

public class Solution {
    public static void main(String[] args) {
        // 1.素组快速排序
        int[] arrayA = new int[10];
        // 创建Random对象(用于生成随机数)
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            arrayA[i] = random.nextInt(10);
            System.out.print(arrayA[i]);
            System.out.print(" ");
        }
        System.out.println();
        Arrays.sort(arrayA);
        for (int i = 0; i < 10; i++) {
            System.out.print(arrayA[i]);
            System.out.print(" ");
        }
    }
}

验证:关键排序方法为Arrays.sort(arrayA);

自定义对象快速排序

关键点,自定义排序方法按照Collections.sort(personList, new Comparator<Person>()定义的内部类规则排序。

复制代码
package com.hmall.user.config;

import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Solution {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        Person person1 = new Person();
        person1.setAge(10);
        person1.setHeight(198);
        person1.setName("阿里");

        Person person2 = new Person();
        person2.setAge(11);
        person2.setHeight(189);
        person2.setName("腾讯");

        Person person3 = new Person();
        person3.setAge(12);
        person3.setHeight(180);
        person3.setName("字节");

        Person person4 = new Person();
        person4.setAge(12);
        person4.setHeight(181);
        person4.setName("字节");

        personList.add(person1);
        personList.add(person2);
        personList.add(person3);

        // 1.按照年龄从大到小排序,按照身高从高到小排序
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.getAge() != o2.getAge()) {
                    return o2.getAge() - o1.getAge();
                } else {
                    return o2.getHeight() - o1.getHeight();
                }
            }
        });
    }
}

@Data
class Person {
    private int age;
    private int height;
    private String name;
}

验证;

字典排序

  • compare(o1, o2)返回值:负数 →o1 在前, →位置不变,正数→o1 在后;

  • 代码中o1.compareTo(o2)是 Java 默认的字典序比较,基于字符的 Unicode 编码值;

  • 最终排序结果是按字典序升序排列:数字(编码小)→小写字母(编码大),同类型字符按 a-z 顺序。

    package com.hmall.user.config;

    import java.util.*;

    public class Solution {
    public static void main(String[] args) {
    List arrayA = new ArrayList<>();
    arrayA.add("ertyuy");
    arrayA.add("iooik");
    arrayA.add("1");
    System.out.println(arrayA);
    Collections.sort(arrayA, new Comparator() {
    @Override
    public int compare(String o1, String o2) {
    return o1.compareTo(o2);
    }
    });
    System.out.println(arrayA);
    }
    }

验证:

相关推荐
SL-staff9 分钟前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
Jerry14 分钟前
LeetCode 383. 赎金信
算法
聪明的一休丶26 分钟前
VLLM v0.24.0 版本深度解析:新引擎、新架构与大规模服务全家桶升级
python·架构·vllm
ai产品老杨26 分钟前
H264 H265视频分析常见问题和排查清单
人工智能·算法·音视频
咖啡八杯41 分钟前
GoF设计模式——模板方法模式
java·后端·spring·设计模式
Jerry1 小时前
LeetCode 454. 四数相加 II
算法
爱笑的源码基地1 小时前
一款全开源的信创云PACS影像云平台解决方案,支持多模态医学影像处理(CT/MR/DR/超声/病理等)
java·源码·国产化·pacs·云影像·区域pacs
可编程芯片开发1 小时前
基于CPS-SPWM链式STATCOM系统在电压不平衡环境下控制策略的simulink建模与仿真
算法
我命由我123452 小时前
Android 开发问题:ClickableSpan 的点击事件没有生效
java·java-ee·android studio·android jetpack·android-studio·android runtime
2zcode2 小时前
基于MATLAB图像处理的饮料瓶灌装液位检测系统设计与实现
开发语言·图像处理·matlab