简体   繁体   中英

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. I am aware of the built-in sort function on python but I would rather use bubble sort or even quicksort for that matter. 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']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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