0
0
NumPydata~10 mins

Sorting along axes in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sorting along axes
Start with 2D array
Choose axis to sort
Sort elements along axis
Return sorted array
End
We start with a 2D array, pick an axis (0 for columns, 1 for rows), sort elements along that axis, and get a sorted array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[3,1,2],[6,4,5]])
sorted_axis0 = np.sort(arr, axis=0)
sorted_axis1 = np.sort(arr, axis=1)
This code sorts a 2D array first by columns (axis=0) and then by rows (axis=1).
Execution Table
StepArray StateAxisActionResult
1[[3,1,2],[6,4,5]]0Sort each columnColumn 1: [3,6] -> [3,6] Column 2: [1,4] -> [1,4] Column 3: [2,5] -> [2,5] Result: [[3,1,2],[6,4,5]]
2[[3,1,2],[6,4,5]]0Sort columns ascending[[3,1,2],[6,4,5]]
3[[3,1,2],[6,4,5]]1Sort each rowRow 1: [3,1,2] -> [1,2,3] Row 2: [6,4,5] -> [4,5,6] Result: [[1,2,3],[4,5,6]]
4[[3,1,2],[6,4,5]]1Sort rows ascending[[1,2,3],[4,5,6]]
5--EndSorting complete
💡 All rows and columns sorted along specified axes, process ends.
Variable Tracker
VariableStartAfter axis=0 sortAfter axis=1 sortFinal
arr[[3,1,2],[6,4,5]][[3,1,2],[6,4,5]][[3,1,2],[6,4,5]][[3,1,2],[6,4,5]]
sorted_axis0-[[3,1,2],[6,4,5]]-[[3,1,2],[6,4,5]]
sorted_axis1--[[1,2,3],[4,5,6]][[1,2,3],[4,5,6]]
Key Moments - 3 Insights
Why does sorting along axis=0 sort columns, not rows?
Sorting along axis=0 means sorting down each column because axis=0 refers to the vertical direction. See execution_table rows 1 and 2 where columns are sorted.
Why does sorting along axis=1 sort rows individually?
Axis=1 means sorting along the horizontal direction, so each row is sorted separately. See execution_table row 3 where each row is sorted.
Does the original array change after sorting?
No, np.sort returns a new sorted array and does not modify the original. See variable_tracker where 'arr' stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the sorted first row?
A[1, 2, 3]
B[3, 1, 2]
C[6, 4, 5]
D[4, 5, 6]
💡 Hint
Check execution_table row 3 under 'Result' for sorted rows.
At which step does sorting along axis=1 complete?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table rows 3 and 4 for axis=1 sorting progress.
If we change axis=0 to axis=1 in np.sort, what changes in variable_tracker?
ABoth sorted_axis0 and sorted_axis1 remain unchanged
Bsorted_axis1 will have sorted columns instead of rows
Csorted_axis0 will have sorted rows instead of columns
DOriginal array 'arr' will be modified
💡 Hint
Refer to variable_tracker and concept_flow about axis meaning.
Concept Snapshot
np.sort(array, axis)
- axis=0 sorts each column (downwards)
- axis=1 sorts each row (across)
- Returns a new sorted array
- Original array stays unchanged
- Useful for sorting multi-dimensional data
Full Transcript
We start with a 2D numpy array. Sorting along axis=0 means sorting each column independently, vertically. Sorting along axis=1 means sorting each row independently, horizontally. The np.sort function returns a new sorted array without changing the original. We saw step-by-step how the array changes after sorting along each axis. This helps organize data by rows or columns as needed.