排序算法举例

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

数组快速排序

复制代码
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<String> arrayA = new ArrayList<>();
    arrayA.add("ertyuy");
    arrayA.add("iooik");
    arrayA.add("1");
    System.out.println(arrayA);
    Collections.sort(arrayA, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
    return o1.compareTo(o2);
    }
    });
    System.out.println(arrayA);
    }
    }

验证:

相关推荐
码云数智-大飞13 分钟前
C++ RAII机制:资源管理的“自动化”哲学
java·服务器·php
2601_9498165818 分钟前
Spring+Quartz实现定时任务的配置方法
java
List<String> error_P29 分钟前
蓝桥杯最后几天冲刺:暴力大法(一)
算法·职场和发展·蓝桥杯
白毛大侠35 分钟前
理解 Go 接口:eface 与 iface 的区别及动态性解析
开发语言·网络·golang
李昊哲小课1 小时前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl
Hou'1 小时前
从0到1的C语言传奇之路
c语言·开发语言
不知名的老吴1 小时前
返回None还是空集合?防御式编程的关键细节
开发语言·python
计算机毕设指导61 小时前
基于SpringBoot校园学生健康监测管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
mysuking1 小时前
springboot与springcloud对应版本
java·spring boot·spring cloud
希望永不加班1 小时前
SpringBoot 数据库连接池配置(HikariCP)最佳实践
java·数据库·spring boot·后端·spring