You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中基于不同属性的对象数组通用冒泡排序实现

通用冒泡排序适配多属性排序方案

要实现不用为每个属性重写冒泡排序,核心是把属性提取逻辑和排序逻辑解耦——让冒泡排序函数接受一个「属性获取函数」作为参数,用这个函数来动态获取要比较的属性值,而不是在排序函数里硬编码某个getter方法。


Java 实现示例

1. 原有代码问题

假设你的EuroPlayer类和原排序函数是这样的:

public class EuroPlayer {
    private String name;
    private String position;
    private String nationality;
    private int goals;

    // 构造函数
    public EuroPlayer(String name, String position, String nationality, int goals) {
        this.name = name;
        this.position = position;
        this.nationality = nationality;
        this.goals = goals;
    }

    // 各属性getter
    public String getName() { return name; }
    public String getPosition() { return position; }
    public String getNationality() { return nationality; }
    public int getGoals() { return goals; }
}

// 原按姓名排序的冒泡函数
public static void bubbleSortByName(EuroPlayer[] players) {
    int n = players.length;
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            // 硬编码调用getName(),只能按姓名排序
            if (players[j].getName().compareTo(players[j+1].getName()) > 0) {
                EuroPlayer temp = players[j];
                players[j] = players[j+1];
                players[j+1] = temp;
            }
        }
    }
}

2. 修改为通用排序函数

引入Function接口接收属性获取逻辑,修改后的函数可适配任意可比较属性:

import java.util.function.Function;

public static void bubbleSort(EuroPlayer[] players, Function<EuroPlayer, Comparable> propertyGetter) {
    int n = players.length;
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            // 通过传入的getter动态获取属性值
            Comparable currentProp = propertyGetter.apply(players[j]);
            Comparable nextProp = propertyGetter.apply(players[j+1]);
            // 比较并交换
            if (currentProp.compareTo(nextProp) > 0) {
                EuroPlayer temp = players[j];
                players[j] = players[j+1];
                players[j+1] = temp;
            }
        }
    }
}

3. 调用方式

通过方法引用传入不同的getter,实现按任意属性排序:

// 按姓名排序
bubbleSort(players, EuroPlayer::getName);
// 按进球数排序
bubbleSort(players, EuroPlayer::getGoals);
// 按位置排序
bubbleSort(players, EuroPlayer::getPosition);

Python 实现示例

1. 原有代码问题

假设你的EuroPlayer类和原排序函数:

class EuroPlayer:
    def __init__(self, name, position, nationality, goals):
        self.name = name
        self.position = position
        self.nationality = nationality
        self.goals = goals

# 原按姓名排序的冒泡函数
def bubble_sort_by_name(players):
    n = len(players)
    for i in range(n-1):
        for j in range(n-i-1):
            if players[j].name > players[j+1].name:
                players[j], players[j+1] = players[j+1], players[j]

2. 修改为通用排序函数

添加参数接收属性获取的lambda或函数:

def bubble_sort(players, get_property):
    n = len(players)
    for i in range(n-1):
        for j in range(n-i-1):
            # 动态获取要比较的属性值
            current_prop = get_property(players[j])
            next_prop = get_property(players[j+1])
            if current_prop > next_prop:
                players[j], players[j+1] = players[j+1], players[j]

3. 调用方式

用lambda表达式传入不同的属性获取逻辑:

# 按姓名排序
bubble_sort(players, lambda p: p.name)
# 按进球数排序
bubble_sort(players, lambda p: p.goals)
# 按国籍排序
bubble_sort(players, lambda p: p.nationality)

核心逻辑说明

  • 这种方式把排序的核心交换逻辑和属性选择逻辑分开,冒泡排序只负责完成通用排序流程,不关心具体用哪个属性比较。
  • 后续新增属性时,无需修改排序函数,只需传入对应的属性获取方式即可。
  • 如果需要支持降序排序,可额外添加参数(比如boolean isDescending),在比较时反转判断条件即可。

内容的提问来源于stack exchange,提问作者Daniel McGee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 00:49:52