What will the following code output?
```python
import numpy as np
arr = np.array([5, 2, 8, 1, 3])
arr.sort()
print(arr)
```
Analysis & Theory
`arr.sort()` sorts the array in place, modifying `arr`. The sorted array is `[1, 2, 3, 5, 8]`.
What does `np.sort()` return when applied to a NumPy array?
```python
import numpy as np
arr = np.array([10, 20, 5, 30])
sorted_arr = np.sort(arr)
print(sorted_arr)
```
A
A) The array itself is sorted in place.
B
B) It returns a new sorted array.
C
C) It sorts the array and raises an error.
D
D) It modifies the original array and returns `None`.
Analysis & Theory
`np.sort()` returns a new sorted array, leaving the original array unchanged. The sorted array is `[5, 10, 20, 30]`.
What is the default sorting order in NumPy's `sort()` function?
```python
import numpy as np
arr = np.array([3, 1, 2])
arr.sort()
print(arr)
```
D
D) It depends on the array's values.
Analysis & Theory
`np.sort()` sorts the array in ascending order by default. So, the result is `[1, 2, 3]`.
What will be the result of sorting a 2D array along axis 0?
```python
import numpy as np
arr = np.array([[3, 2], [1, 4], [5, 6]])
sorted_arr = np.sort(arr, axis=0)
print(sorted_arr)
```
A
A) `[[1, 2], [3, 4], [5, 6]]`
B
B) `[[1, 4], [3, 2], [5, 6]]`
C
C) `[[1, 2], [3, 4], [5, 6]]`
D
D) `[[1, 2], [3, 4], [5, 6]]`
Analysis & Theory
When sorting along axis 0 (rows), the elements are sorted in each column individually. The result is `[[1, 2], [3, 4], [5, 6]]`.
What happens when you apply `np.sort()` to a 2D array along axis 1?
```python
import numpy as np
arr = np.array([[5, 3, 1], [4, 2, 6]])
sorted_arr = np.sort(arr, axis=1)
print(sorted_arr)
```
A
A) It sorts each row independently, so the result is `[[1, 3, 5], [2, 4, 6]]`.
B
B) It sorts each column independently, so the result is `[[1, 2], [3, 4], [5, 6]]`.
C
C) It raises a `ValueError` due to axis mismatch.
D
D) It sorts the entire array as a single row.
Analysis & Theory
When sorting along axis 1 (columns), the elements are sorted within each row independently. The result is `[[1, 3, 5], [2, 4, 6]]`.
What does the `np.argsort()` function return?
```python
import numpy as np
arr = np.array([30, 10, 20, 40])
result = np.argsort(arr)
print(result)
```
A
A) The indices of the sorted elements.
B
B) The sorted elements themselves.
C
C) A new array with the same shape.
D
D) A copy of the original array.
Analysis & Theory
`np.argsort()` returns the indices that would sort the array. For `arr = [30, 10, 20, 40]`, the result is `[1, 2, 0, 3]`.
What does `np.lexsort()` do?
```python
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([10, 20, 30])
result = np.lexsort((arr2, arr1))
print(result)
```
A
A) It sorts based on the values of multiple arrays.
B
B) It sorts the arrays independently.
C
C) It finds the lexicographically smallest array.
D
D) It returns the sorted values from the array.
Analysis & Theory
`np.lexsort()` sorts based on multiple arrays in lexicographical order. Here, it sorts `arr2` first, and then `arr1` if there are ties.
What happens when you pass `np.sort()` a multi-dimensional array with NaN values?
```python
import numpy as np
arr = np.array([10, np.nan, 5, np.nan])
sorted_arr = np.sort(arr)
print(sorted_arr)
```
A
A) NaN values are treated as the highest values and placed at the end.
B
B) NaN values are treated as the lowest values and placed at the beginning.
C
C) NaN values are ignored during sorting.
D
D) The function raises a `ValueError`.
Analysis & Theory
In NumPy, `NaN` values are treated as larger than any other number when sorting, so they are placed at the end of the sorted array.
Which of the following functions sorts a 2D array based on values in a specific column?
```python
import numpy as np
arr = np.array([[2, 3], [1, 4], [3, 2]])
sorted_arr = np.sort(arr, axis=0)
print(sorted_arr)
```
D
D) `np.sort()` with `axis=1`
Analysis & Theory
`np.sort()` with `axis=0` sorts each column independently, resulting in sorting based on values in specific columns.
What is the result of the following code?
```python
import numpy as np
arr = np.array([5, 2, 9, 1])
arr.sort(axis=None)
print(arr)
```
Analysis & Theory
`np.sort()` with `axis=None` sorts the entire array, resulting in `[1, 2, 5, 9]`.