Bird
Raised Fist0
NumPydata~20 mins

np.sort() for sorting arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Sorting Master with np.sort()
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.sort() on a 1D array
What is the output of this code?
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.sort(arr)
print(sorted_arr)
NumPy
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.sort(arr)
print(sorted_arr)
A[5 4 3 1 1]
B[1 1 3 4 5]
C[3 1 4 1 5]
D[1 3 4 5]
Attempts:
2 left
💡 Hint
np.sort() returns a sorted copy of the array in ascending order.
❓ data_output
intermediate
2:00remaining
Sorting a 2D array along axis 0
Given this 2D array, what is the output of np.sort(arr, axis=0)?
import numpy as np
arr = np.array([[3, 2], [1, 4]])
sorted_arr = np.sort(arr, axis=0)
print(sorted_arr)
NumPy
import numpy as np
arr = np.array([[3, 2], [1, 4]])
sorted_arr = np.sort(arr, axis=0)
print(sorted_arr)
A
[[3 4]
 [1 2]]
B
[[3 2]
 [1 4]]
C
[[1 2]
 [3 4]]
D
[[1 4]
 [3 2]]
Attempts:
2 left
💡 Hint
Sorting along axis 0 sorts each column independently.
🔧 Debug
advanced
2:00remaining
Identify the error in using np.sort()
What error does this code raise?
import numpy as np
arr = np.array([3, 1, 4])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
NumPy
import numpy as np
arr = np.array([3, 1, 4])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
AAxisError: axis 1 is out of bounds for array of dimension 1
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: could not broadcast input array from shape (3,) into shape (1,)
DNo error, prints [1 3 4]
Attempts:
2 left
💡 Hint
Check the dimension of the array and the axis parameter.
🚀 Application
advanced
2:30remaining
Using np.sort() to sort structured arrays
Given this structured array, what is the output of np.sort(arr, order='age')?
import numpy as np
arr = np.array([(1, 'Alice', 25), (2, 'Bob', 20), (3, 'Carol', 30)],
               dtype=[('id', 'i4'), ('name', 'U10'), ('age', 'i4')])
sorted_arr = np.sort(arr, order='age')
print(sorted_arr['name'])
NumPy
import numpy as np
arr = np.array([(1, 'Alice', 25), (2, 'Bob', 20), (3, 'Carol', 30)],
               dtype=[('id', 'i4'), ('name', 'U10'), ('age', 'i4')])
sorted_arr = np.sort(arr, order='age')
print(sorted_arr['name'])
A['Bob' 'Alice' 'Carol']
B['Alice' 'Bob' 'Carol']
C['Carol' 'Alice' 'Bob']
D['Bob' 'Carol' 'Alice']
Attempts:
2 left
💡 Hint
Sorting by 'age' orders the records by the age field ascending.
🧠 Conceptual
expert
1:30remaining
Effect of np.sort() on original array
Which statement about np.sort() is true?
Anp.sort() only works on 1D arrays.
Bnp.sort() sorts the array in place and returns None.
Cnp.sort() modifies the original array and returns the sorted array.
Dnp.sort() returns a sorted copy and does not change the original array.
Attempts:
2 left
💡 Hint
Think about whether np.sort() changes the original array or not.

Practice

(1/5)
1. What does the np.sort() function do when applied to a NumPy array?
easy
A. It returns a new array with elements sorted in ascending order.
B. It changes the original array to sorted order in place.
C. It reverses the order of elements in the array.
D. It removes duplicate elements from the array.

Solution

  1. Step 1: Understand np.sort() behavior

    The np.sort() function returns a new sorted array and does not modify the original array.
  2. Step 2: Identify the sorting order

    By default, np.sort() sorts elements in ascending order.
  3. Final Answer:

    It returns a new array with elements sorted in ascending order. -> Option A
  4. Quick Check:

    np.sort() returns sorted copy [OK]
Hint: np.sort() returns a new sorted array, original stays same [OK]
Common Mistakes:
  • Thinking np.sort() sorts in place
  • Confusing sorting with reversing
  • Assuming it removes duplicates
2. Which of the following is the correct syntax to sort a 1D NumPy array named arr using np.sort()?
easy
A. np.sort(arr)
B. arr.sort()
C. np.sort(arr, axis=1)
D. arr.sorted()

Solution

  1. Step 1: Recall np.sort() syntax

    The correct way to sort an array using the function is np.sort(arr).
  2. Step 2: Check other options

    arr.sort() sorts in place but is a method, not np.sort(). np.sort(arr, axis=1) is invalid for 1D arrays. arr.sorted() is not a valid method.
  3. Final Answer:

    np.sort(arr) -> Option A
  4. Quick Check:

    np.sort(arr) is correct syntax [OK]
Hint: Use np.sort(array) to get sorted copy [OK]
Common Mistakes:
  • Using arr.sorted() which does not exist
  • Using axis=1 on 1D array
  • Confusing np.sort() with arr.sort() method
3. What is the output of the following code?
import numpy as np
arr = np.array([[3, 1, 2], [6, 4, 5]])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
medium
A. [[1 3 2] [4 6 5]]
B. [[3 1 2] [6 4 5]]
C. [[1 2 3] [4 5 6]]
D. [[3 6] [1 4] [2 5]]

Solution

  1. Step 1: Understand sorting along axis=1

    Sorting with axis=1 sorts each row independently in ascending order.
  2. Step 2: Sort each row

    First row [3,1,2] sorted is [1,2,3]. Second row [6,4,5] sorted is [4,5,6].
  3. Final Answer:

    [[1 2 3] [4 5 6]] -> Option C
  4. Quick Check:

    Row-wise sort = [[1 2 3], [4 5 6]] [OK]
Hint: axis=1 sorts each row separately [OK]
Common Mistakes:
  • Sorting columns instead of rows
  • Expecting original array unchanged in print
  • Confusing axis parameter meaning
4. The following code throws an error. What is the cause?
import numpy as np
arr = np.array([3, 1, 2])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
medium
A. np.sort() cannot sort integer arrays.
B. Missing parentheses in np.sort call.
C. The array must be converted to a list first.
D. Axis 1 does not exist for 1D arrays.

Solution

  1. Step 1: Check array dimensions

    The array arr is 1D, so it only has axis 0.
  2. Step 2: Understand axis parameter

    Using axis=1 on a 1D array causes an error because axis 1 does not exist.
  3. Final Answer:

    Axis 1 does not exist for 1D arrays. -> Option D
  4. Quick Check:

    1D array has only axis 0 [OK]
Hint: 1D arrays only have axis=0, axis=1 causes error [OK]
Common Mistakes:
  • Assuming axis=1 works on 1D arrays
  • Thinking np.sort can't handle integers
  • Believing array must be list to sort
5. Given a 2D NumPy array data = np.array([[7, 2, 9], [4, 5, 1], [8, 3, 6]]), how can you sort the entire array as if it were a flat list, then reshape it back to the original shape?
hard
A. np.sort(data, axis=0).reshape(data.shape)
B. np.sort(data, axis=None).reshape(data.shape)
C. data.sort(axis=1).reshape(data.shape)
D. np.sort(data).reshape(data.shape)

Solution

  1. Step 1: Flatten and sort the entire array

    Using axis=None in np.sort() sorts the array as a flat 1D array.
  2. Step 2: Reshape sorted array back to original shape

    Use .reshape(data.shape) to restore the 2D shape after sorting.
  3. Final Answer:

    np.sort(data, axis=None).reshape(data.shape) -> Option B
  4. Quick Check:

    axis=None sorts flat, reshape restores shape [OK]
Hint: Use axis=None to sort flat, then reshape [OK]
Common Mistakes:
  • Sorting only rows or columns instead of flat
  • Using data.sort() which sorts in place
  • Omitting reshape after sorting flat