排序算法举例

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

数组快速排序

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

验证:

相关推荐
阿里嘎多学长1 小时前
2026-10-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
曹牧1 小时前
C#:空合并运算符
java·前端·数据库
happylifetree2 小时前
Python67:核心语法-函数进阶-参数类型(函数作为参数)
python
布吉岛的石头2 小时前
Java 程序员第 50 阶段2:预训练的目标函数:自回归语言建模本质
java·开发语言·人工智能·深度学习·回归·transformer
golang学习记2 小时前
IDEA 2026年的玩法已经完全不同
java·ide·intellij-idea
小羊没烦恼!2 小时前
铁道部新客票系统设计(二)
开发语言·前端·数据库·python·oracle·c#·php
紧跟先前的步伐2 小时前
Go AI Agent 实战 05|Temperature 与生成参数:让模型输出可控
java·人工智能·golang
金銀銅鐵2 小时前
[Java] 展示添加元素时 ArrayList 的内部变化
java·后端·ai编程
斌糖雪梨3 小时前
spring auth 核心流程详解
java·后端·spring
quantdash_cc3 小时前
从实时行情到价格观察表:用 Python 和 Pandas 构建策略输入数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash