Sorted elements of a given array using NumPy Write a program to get the indices of the sorted elements of a given array Hint: Use argsort function Sample data: [1023 5202 6230 1671 1682 5241 4532] Sample output:[0346152]
时间: 2024-02-03 13:02:07 浏览: 87
c语言-leetcode 0004-median-of-two-sorted-array.zip
Certainly, here's the code to get the indices of sorted elements of a given array using the `argsort` function in NumPy:
```python
import numpy as np
# Sample data
arr = np.array([1023, 5202, 6230, 1671, 1682, 5241, 4532])
# Get the indices of sorted elements
indices = np.argsort(arr)
# Print the sorted indices
print(indices)
```
When you run this code with the sample data, you should get the following output:
```
[0 3 4 6 1 5 2]
```
This means that the smallest element in the array is at index 0, the second smallest element is at index 3, and so on, until the largest element is at index 2.
I hope this helps! Let me know if you have any more questions.
阅读全文