Sorting, Searching & Counting
Sorting, searching, and counting are the tools for putting array data in order and finding out what's inside it — NumPy provides fast, vectorized functions like np.sort , np.argsort , np.searchsorted , and np.unique for exactly this.
Learn Sorting, Searching & Counting in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll order arrays, learn copy-vs-in-place sorting, get sort indices, search sorted data, and count values and non-zeros.
np.sort(arr) returns a new sorted copy and leaves the original untouched. The method arr.sort() sorts in place — it changes the original array and returns None . Choosing the right one avoids surprising bugs.
Expected output: [1 2 3] , then the original still [3 1 2] , and finally [1 2 3] after the in-place sort.
np.argsort(arr) returns the indices that would sort the array. Indexing the array with those indices gives the sorted values — and the same indices can reorder a parallel array, which is how you sort one list by another (e.g. sort names by their scores).
Expected output: [0 2 1] , then [50 70 90] , then ['Ann' 'Cara' 'Bob'] .
For 2D arrays, the axis argument controls the direction. axis=1 sorts each row left to right; axis=0 sorts each column top to bottom. The default is the last axis.
Expected output: rows sorted to [[1 2 3] [7 8 9]] , and columns already in order so axis=0 leaves [[3 1 2] [9 7 8]] unchanged.
np.searchsorted(sorted_arr, value) uses binary search to find the index where value would be inserted to keep the array sorted. For counting, np.unique(arr, return_counts=True) returns the sorted unique values and how often each appears, np.count_nonzero counts non-zero (or True) elements, and np.argmax / np.argmin return the index of the largest / smallest value.
Expected output: 2 (25 fits between 20 and 30), then [0 3 5] for the three queries.
Expected output: values ['a' 'b' 'c'] with counts [3 2 1] , then 3 , 2 , 3 , 0 .
Replace each ___ so the program prints a sorted copy and the unique values with their counts.
Expected output: [1 1 2 2 2 3] , then [1 2 3] and [2 3 1] . (Answers: sort , True .)
arr.sort() sorts in place and returns None , so assigning it wipes out your array.
✅ Fix: call arr.sort() alone, or use arr = np.sort(arr) for a copy.
np.argmax returns the index of the largest value, not the value itself.
✅ Fix: use arr.max() for the value, np.argmax(arr) for its position.
Given test scores, find the three highest and the index of the very best, using sorting and argmax.
Expected output: [99 92 88] , then 5 , then 4 passing scores.
Lesson 18 complete — you can order and analyze data!
You learned copy-vs-in-place sorting, np.argsort , axis sorting, np.searchsorted , and the counting tools np.unique , np.count_nonzero , and argmax/argmin.
🚀 Up next: Structured & Record Arrays — store rows with named, typed fields like a mini database.
Practice quiz
What does np.sort(arr) do?
- Returns a new sorted copy, leaving the original unchanged
- Sorts the original in place
- Returns None
- Reverses the array
Answer: Returns a new sorted copy, leaving the original unchanged. np.sort returns a brand-new sorted copy and leaves the original untouched.
What does the method arr.sort() return?
- A sorted copy
- None (it sorts in place)
- The indices
- The original order
Answer: None (it sorts in place). arr.sort() sorts in place and returns None, so arr = arr.sort() destroys your data.
What does np.argsort(arr) return?
- The sorted values
- The maximum
- The indices that would sort the array
- The counts
Answer: The indices that would sort the array. argsort returns the indices that would sort the array, useful for sorting a parallel array.
Which axis sorts each row of a 2D array left to right?
- axis=0
- axis=None
- axis=2
- axis=1
Answer: axis=1. axis=1 sorts along the columns within each row (left to right).
What does np.searchsorted do?
- Finds the insertion index to keep a sorted array sorted
- Sorts in reverse
- Counts elements
- Removes duplicates
Answer: Finds the insertion index to keep a sorted array sorted. searchsorted uses binary search to find where a value would be inserted to stay sorted.
How do you get unique values with their counts?
- np.sort(arr)
- np.unique(arr, return_counts=True)
- np.argsort(arr)
- np.count_nonzero(arr)
Answer: np.unique(arr, return_counts=True). np.unique with return_counts=True returns sorted unique values and their counts.
What does np.argmax(arr) return?
- The maximum value
- The sum
- The index of the largest value
- The sorted array
Answer: The index of the largest value. argmax returns the index of the largest value, not the value itself.
What does np.count_nonzero(arr > 4) count?
- The total elements
- The maximum
- The first index
- How many elements are greater than 4
Answer: How many elements are greater than 4. Applied to a boolean mask, count_nonzero counts the True elements, i.e. those greater than 4.
Why prefer np.sort(arr) when you must keep the original order?
- It returns a copy and leaves the original intact
- It is faster
- It sorts in reverse
- It removes duplicates
Answer: It returns a copy and leaves the original intact. np.sort produces a new array, so the original ordering is preserved.
What does arr.max() give compared to np.argmax(arr)?
- The index, while argmax gives the value
- Nothing different
- The value, while argmax gives its index
- Both give the index
Answer: The value, while argmax gives its index. arr.max() returns the value; np.argmax returns its position.