如何使用python对2D数组进行冒泡排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用python对2D数组进行冒泡排序相关的知识,希望对你有一定的参考价值。

我有此列表,总结了2018年NHL球员的一些数据。我想按pts对其进行排序,这是使用泡沫的第7个值。我知道python上内置的sort函数,但是我更愿意使用冒泡排序甚至quicksort。任何人都可以帮忙吗?

[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]

这是我到目前为止所做的:

def sortByPoints(stats):

    lengthOfstats = len(stats) - 1
    for i in range(lengthOfstats):
        for j in range(lengthOfstats - i):
            if stats[j] < stats[j + 1]:
                stats[j], stats[j + 1] = stats[j + 1], stats[j]

    return stats

print(sortByPoints(readStatsFromFile()))
答案

创建可以根据子数组索引对嵌套数组进行排序的冒泡排序

BubbleSort的修改

def bubbleSort(arr, ind = 6):
    """Bubble sort arr based upon subelement ind (default of index 6) 
       which is 7th element of sub-array since 0 based indexing"""
    n = len(arr)

    # Traverse through all array elements
    for i in range(n):

        # Last i elements are already in place
        for j in range(0, n-i-1):

            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if int(arr[j][ind]) > int(arr[j+1][ind]) :
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

Test

arr = [['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]

import pprint
print('pre-sorted')
pprint.pprint(arr)
print('sorted')
pprint.pprint(bubbleSort(arr))

输出

 pre-sorted
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], 
 ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], 
 ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], 
 ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], 
 ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], 
 ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]

sorted
[['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], 
 ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], 
 ['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], 
 ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15'], 
 ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], 
 ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88']]

以上是关于如何使用python对2D数组进行冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章

冒泡排序仅对数组的一部分进行排序

特么,冒泡排序有这么难?

特么,冒泡排序怎么可以这么难?

C++ 使用冒泡排序根据第一列对二维数组的行进行排序

在Ruby中对数组使用冒泡排序方法[关闭]

冒泡排序 和 选择排序的 区别 python