codecodecodecode
codecodecodecode

Reputation: 3

Python insertion/selection sort 2d arrays

Hello everyone I'm working on a project that will time how fast each sort method sorts a 2d array of numbers. However I can't seem to find any methods that work for sorting 2d arrays. my selection sort looks like:

def selection(arr): 
    for i in range(len(arr)): 
        min_idx = i 
        for j in range(i+1, len(arr)): 
            if arr[min_idx] > arr[j]: 
                min_idx = j 
                
        arr[i], arr[min_idx] = arr[min_idx], arr[i] 

and my insertion code looks like:

def insertionSort(arr): 
    for i in range(1, len(arr)): 
        key = arr[i] 
        j = i-1
        while j >=0 and key < arr[j] : 
                arr[j+1] = arr[j] 
                j -= 1
        arr[j+1] = key

and how I'm creating the 2d array is:

arrayInput = int(input("please input the number of arrays: \n"))
amountInput = int(input("please enter how many numbers per array: \n"))

arr = np.empty((arrayInput, amountInput))
x = arr
y = x

for i in range(arrayInput):
    for j in range(amountInput):
        arr[i][j] = random.randint(0,100) 

I was wondering how I would be able to convert my selection and insertion sorts functions to be able to sort 2d array. so if my input is array =

input = [
    [2,4,3,1,5], 
    [3,4,5,2,3]
]

I want the functions to sort the arrays with insertion and selection to output

expected_output = [
    [1,2,3,4,5], 
    [2,3,3,4,5] 
]

Apologies if I got some terminology incorrect.

Upvotes: 0

Views: 411

Answers (1)

user9706
user9706

Reputation:

As you already have functions to sort an array, I would write a sort2d function that sorts each row like this given one of those functions:

def sort2d(arr, sort):
   for row in range(len(arr)):
      sort(arr[row])

selection2d = lambda arr: sort2d(arr, selection)
insertionSort2d = lambda arr: sort2d(arr, insertionSort)

arr = [[2,4,3,1,5], [3,4,5,2,3]]
selection2d(arr)
print(arr)
[[1, 2, 3, 4, 5], [2, 3, 3, 4, 5]]

You clarified that you wanted to modify the functions themselves, so you inline the sort2d code shown either by using an local function:

 def selection2d(arr);
     def selection(arr): 
         for i in range(len(arr)):
             min_idx = i 
             for j in range(i+1, len(arr)): 
                  if arr[min_idx] > arr[j]: 
                     min_idx = j          
          arr[i], arr[min_idx] = arr[min_idx], arr[i]

      for row in range(len(arr)):
          selection(arr[row])

And if you don't like that add for row in range(len(arr2d)): into selection then change existing reference to arr to arr[row]:

def selection(arr): 
    for row in range(len(arr)):
        for i in range(len(arr[row])): 
            min_idx = i 
            for j in range(i+1, len(arr[row])): 
                if arr[row][min_idx] > arr[row][j]: 
                    min_idx = j 
            arr[row][i], arr[row][min_idx] = arr[row][min_idx], arr[row][i]

Upvotes: 0

Related Questions