Bird
Raised Fist0
NumPydata~10 mins

np.sort() for sorting arrays in NumPy - Step-by-Step Execution

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
Concept Flow - np.sort() for sorting arrays
Input Array
↓
Call np.sort()
↓
Compare elements
↓
Rearrange elements in order
↓
Return sorted array
np.sort() takes an array, compares elements, rearranges them in ascending order, and returns the sorted array.
Execution Sample
NumPy
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.sort(arr)
print(sorted_arr)
This code sorts the array [3, 1, 4, 1, 5] in ascending order using np.sort() and prints the sorted array.
Execution Table
StepArray StateActionResult
1[3, 1, 4, 1, 5]Start sorting[3, 1, 4, 1, 5]
2[3, 1, 4, 1, 5]Compare 3 and 1, swap[1, 3, 4, 1, 5]
3[1, 3, 4, 1, 5]Compare 3 and 4, no swap[1, 3, 4, 1, 5]
4[1, 3, 4, 1, 5]Compare 4 and 1, swap[1, 3, 1, 4, 5]
5[1, 3, 1, 4, 5]Compare 4 and 5, no swap[1, 3, 1, 4, 5]
6[1, 3, 1, 4, 5]Compare 3 and 1, swap[1, 1, 3, 4, 5]
7[1, 1, 3, 4, 5]No more swaps needed[1, 1, 3, 4, 5]
8[1, 1, 3, 4, 5]Return sorted array[1, 1, 3, 4, 5]
💡 Sorting complete, array is in ascending order.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
arr[3, 1, 4, 1, 5][3, 1, 4, 1, 5][3, 1, 4, 1, 5][3, 1, 4, 1, 5][3, 1, 4, 1, 5]
sorted_arrN/AN/AN/AN/A[1, 1, 3, 4, 5]
Key Moments - 3 Insights
Why does np.sort() return a new array instead of changing the original?
np.sort() returns a sorted copy and does not change the original array, as shown in the variable_tracker where 'arr' stays the same and 'sorted_arr' holds the sorted result.
What happens if the array has repeated numbers?
Repeated numbers stay in the sorted array in order, as seen in the execution_table where '1' appears twice and remains after sorting.
Does np.sort() sort in ascending or descending order by default?
np.sort() sorts in ascending order by default, as shown in the final sorted array [1, 1, 3, 4, 5] in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the array state after comparing 4 and 1?
A[1, 3, 4, 1, 5]
B[1, 3, 1, 4, 5]
C[3, 1, 4, 1, 5]
D[1, 1, 3, 4, 5]
💡 Hint
Check the 'Array State' column at Step 4 in the execution_table.
At which step does the array become fully sorted?
AStep 7
BStep 5
CStep 6
DStep 8
💡 Hint
Look for the step where 'No more swaps needed' is noted in the Action column.
If the original array was already sorted, how would the execution_table change?
AThe array would be empty.
BThe array would be reversed.
CThere would be no swaps, only comparisons.
DThe sorting would fail.
💡 Hint
Think about what happens when elements are already in order during sorting.
Concept Snapshot
np.sort(array) → returns a sorted copy of the array in ascending order.
Original array stays unchanged.
Works on 1D and multi-dimensional arrays.
Default sorting is ascending.
Use sorted_arr = np.sort(arr) to keep sorted result.
Full Transcript
np.sort() is a function in numpy that sorts arrays. It takes an input array and returns a new array with elements arranged in ascending order. The original array remains unchanged. The sorting process compares elements and swaps them if needed until the entire array is sorted. For example, sorting [3, 1, 4, 1, 5] results in [1, 1, 3, 4, 5]. Repeated numbers stay in the sorted array. The function works on arrays of any shape but returns a sorted copy. This visual trace shows each comparison and swap step, how the array changes, and when sorting finishes.

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