简体   繁体   English

如何使用python对二维数组进行冒泡排序

[英]How to bubble sort a 2D array with python

I have this list that's a summary of a few NHL player stats in 2018. I want to sort them by pts which is the 7th value using bubble.我有这个列表,它总结了 2018 年的一些 NHL 球员统计数据。我想按 pts 对它们进行排序,这是使用气泡的第 7 个值。 I am aware of the built-in sort function on python but I would rather use bubble sort or even quicksort for that matter.我知道 python 上的内置排序功能,但我更愿意使用冒泡排序甚至快速排序。 Can anyone help out?任何人都可以帮忙吗?

[['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']]

This is what I did so far:这是我到目前为止所做的:

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()))

Create bubble sort that can sort nested-arrays based upon an index of sub-array创建可以根据子数组索引对嵌套数组进行排序的冒泡排序

Modification of 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))

Output输出

 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']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM