Skip to content

Commit 7d1c8f4

Browse files
Create sorting_numpy_array.md
Added Introduction Added sort method Added argsort method Added lexsort method
1 parent ed4b85e commit 7d1c8f4

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

contrib/numpy/sorting_numpy_array.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Sorting NumPy Arrays
2+
- Sorting arrays is a common operation in data manipulation and analysis.
3+
- NumPy provides various functions to sort arrays efficiently.
4+
- The primary methods are `numpy.sort`,`numpy.argsort`, and `numpy.lexsort`
5+
6+
### 1. numpy.sort()
7+
8+
The `numpy.sort` function returns a sorted copy of an array.
9+
10+
#### Syntax :
11+
12+
```python
13+
numpy.sort(arr, axis=-1, kind=None, order=None)
14+
```
15+
- **arr** : Array to be sorted.
16+
- **axis** : Axis along which to sort. (By Default is -1)
17+
- **kind** : Sorting algorithm. Options are 'quicksort', 'mergesort', 'heapsort', and 'stable'. (By Default 'quicksort')
18+
- **order** : When arr is an array with fields defined, this argument specifies which fields to compare first.
19+
20+
#### Example :
21+
22+
```python
23+
import numpy as np
24+
25+
arr = np.array([1,7,0,4,6])
26+
sarr = np.sort(arr)
27+
print(sarr)
28+
```
29+
30+
**Output** :
31+
```python
32+
[0 1 4 6 7]
33+
```
34+
35+
### 2. numpy.argsort()
36+
37+
The `numpy.argsort` function returns the indices that would sort an array. Using those indices you can sort the array.
38+
39+
#### Syntax :
40+
41+
```python
42+
numpy.argsort(a, axis=-1, kind=None, order=None)
43+
```
44+
- **arr** : Array to be sorted.
45+
- **axis** : Axis along which to sort. (By Default is -1)
46+
- **kind** : Sorting algorithm. Options are 'quicksort', 'mergesort', 'heapsort', and 'stable'. (By Default 'quicksort')
47+
- **order** : When arr is an array with fields defined, this argument specifies which fields to compare first.
48+
49+
#### Example :
50+
51+
```python
52+
import numpy as np
53+
54+
arr = np.array([2.1,7,4.2,4.3,6])
55+
indices = np.argsort(arr)
56+
print(indices)
57+
s_arr = arr[indices]
58+
print(s_arr)
59+
```
60+
61+
**Output** :
62+
```python
63+
[0 2 3 4 1]
64+
[2.1 4.2 4.3 6. 7. ]
65+
```
66+
67+
### 3. np.lexsort()
68+
69+
The np.lexsort function performs an indirect stable sort using a sequence of keys.
70+
71+
#### Syntax :
72+
73+
```python
74+
numpy.lexsort(keys, axis=-1)
75+
```
76+
- **keys**: Sequence of arrays to sort by. The last key is the primary sort key.
77+
- **axis**: Axis to be indirectly sorted.(By Default -1)
78+
79+
#### Example :
80+
81+
```python
82+
import numpy as np
83+
84+
a = np.array([5,4,3,2])
85+
b = np.array(['a','d','c','b'])
86+
indices = np.lexsort((a,b))
87+
print(indices)
88+
89+
s_arr = a[indices]
90+
print(s_arr)
91+
92+
s_arr = b[indices]
93+
print(s_arr)
94+
```
95+
96+
**Output** :
97+
```python
98+
[0 3 2 1]
99+
[2 3 4 5]
100+
['a' 'b' 'c' 'd']
101+
```
102+
103+
NumPy provides powerful and flexible functions for sorting arrays, including `np.sort`, `np.argsort`, and `np.lexsort`.
104+
These functions support sorting along different axes, using various algorithms, and sorting by multiple keys, making them suitable for a wide range of data manipulation tasks.

0 commit comments

Comments
 (0)