排序算法举例

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

数组快速排序

复制代码
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);
    }
    }

验证:

相关推荐
Highcharts.js4 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
爱昏羔4 小时前
下篇:从检索到交互 — 物流RAG问答系统与WebUI全实现
python·langchain·agent
_Jimmy_5 小时前
Agent引用数据库知识过时的增量同步方案
人工智能·python·langchain
头发还在的女程序员6 小时前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
CodeStats6 小时前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
min(a,b)6 小时前
学习第 4 天:面向对象与异常处理
python·学习·学习方法
我命由我123457 小时前
Android 开发问题:为 PDFView 设置一个带有黑色边框的背景 drawable,但边框没有生效
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
拳里剑气7 小时前
C++算法:BFS解决FloodFill算法
c++·算法·bfs·宽度优先
爱写代码的小朋友8 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++