Sort and Search
Problem Statement
Sort NumPy arrays and find elements.
Background
- np.sort(arr): Returns sorted copy
- np.argsort(arr): Returns indices that would sort the array
- np.where(condition): Find indices where condition is True
- np.argmax(), np.argmin(): Index of max/min element
Your Task
Write a function sort_and_search(arr) that returns a dictionary with:
- "sorted": Sorted array as list
- "argsort": Indices that would sort the array
- "argmax": Index of maximum value
- "argmin": Index of minimum value
Output Format
Return a dictionary with exactly these four keys.
Example:
[3, 1, 4, 1, 5, 9, 2, 6]
{'sorted': [1, 1, 2, 3, 4, 5, 6, 9], 'argsort': [1, 3, 6, 0, 2, 4, 7, 5], 'argmax': 5, 'argmin': 1}9 is at index 5 (max), first 1 is at index 1 (min)
Constraints:
- Use np.sort(), np.argsort(), np.argmax(), np.argmin()
- Array will have at least 1 element
Sort and Search: Comprehensive Background
1. Background Knowledge
NumPy Array Operations
NumPy is Python's fundamental library for numerical computing, providing efficient array operations that are orders of magnitude faster than native Python lists. NumPy arrays enable vectorized operations—applying functions to entire arrays without explicit loops—which improves both performance and code readability.
Core Sorting Concepts
Sorting is arranging elements in a specific order (ascending or descending). The key insight is that different sorting operations serve different purposes:
- np.sort(arr): Returns a new sorted array without modifying the original
- np.argsort(arr): Returns the indices that would sort the array—crucial for tracking element positions
- np.argmax() / np.argmin(): Find the index of the maximum/minimum value, not the value itself
Why Indices Matter
When you call np.argsort(), you get indices that represent the permutation needed to sort the array. For example, if argsort() returns [1, 3, 6, 0, 2, 4, 7, 5], this means: "take element at index 1 first, then index 3, then index 6," etc., to get a sorted sequence.
2. Algorithm Approach
Sorting Algorithms in NumPy
NumPy's sort() uses Quicksort by default (with fallback to Heapsort for worst-case scenarios), achieving average-case performance of O(nlogn). The underlying implementation is highly optimized in C, making it significantly faster than Python-level sorting.
Index-Based Sorting
np.argsort() performs the same sorting operation but returns indices instead of values. This is essential when you need to:
- Track which original positions elements came from
- Apply the same permutation to multiple arrays
- Maintain relationships between sorted data and metadata
Extrema Finding
np.argmax() and np.argmin() scan the array once in O(n) time to find the index of the maximum/minimum element. These are more efficient than sorting when you only need one extremum.
3. Step-by-Step Strategy
Step 1: Sort the array
sorted_arr = np.sort(arr)
Convert to list for the dictionary output using .tolist().
Step 2: Get sorting indices
indices = np.argsort(arr)
These indices tell you the original positions of elements in sorted order.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.