1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use crateHeap;
/// Sorts a slice using the Bubble Sort algorithm.
///
/// The bubble sort algorithm repeatedly steps through the list, compares adjacent
/// elements, and swaps them if they are in the wrong order. This process continues
/// until the list is sorted.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// # Time Complexity
/// - Best: `O(n)`
/// - Worst: `O(n²)`
/// - Average: `O(n²)`
///
/// # Space Complexity
/// - `O(1)` (in-place sorting)
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::bubble_sort;
///
/// let mut arr = [5, 3, 8, 4, 2];
/// bubble_sort(&mut arr);
/// assert_eq!(arr, [2, 3, 4, 5, 8]);
/// ```
/// Sorts a slice using the Insertion Sort algorithm.
///
/// Insertion sort builds the sorted array one element at a time. It takes each new element
/// and inserts it into its correct position within the sorted portion of the array.
///
/// ### Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// ### Time Complexity
/// - Best: `O(n)`
/// - Worst: `O(n²)`
/// - Average: `O(n²)`
///
/// ### Space Complexity
/// - `O(1)` (in-place sorting)
///
/// ### Examples
/// ```
/// use dsa::algorithms::sorting::insertion_sort;
///
/// let mut arr = [5, 3, 8, 4, 2];
/// insertion_sort(&mut arr);
/// assert_eq!(arr, [2, 3, 4, 5, 8]);
/// ```
/// Sorts a slice using the Selection Sort algorithm.
///
/// Selection sort repeatedly selects the minimum element from the unsorted portion of the array
/// and swaps it with the first unsorted element.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// # Time Complexity
/// - Best: `O(n²)`
/// - Worst: `O(n²)`
/// - Average: `O(n²)`
///
/// # Space Complexity
/// - `O(1)` (in-place sorting)
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::selection_sort;
///
/// let mut arr = [5, 3, 8, 4, 2];
/// selection_sort(&mut arr);
/// assert_eq!(arr, [2, 3, 4, 5, 8]);
/// ```
/// Sorts a slice using the Merge Sort algorithm.
///
/// Merge sort is a divide-and-conquer algorithm that splits the array into two halves,
/// recursively sorts them, and then merges the sorted halves.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// # Time Complexity
/// - Best: `O(n log n)`
/// - Worst: `O(n log n)`
/// - Average: `O(n log n)`
///
/// # Space Complexity
/// - `O(n)` (requires additional space for merging)
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::merge_sort;
///
/// let mut arr = [5, 3, 8, 4, 2];
/// merge_sort(&mut arr);
/// assert_eq!(arr, [2, 3, 4, 5, 8]);
/// ```
/// Merges two sorted halves of a slice.
///
/// # Parameters
/// - `arr`: The slice to merge the halves into.
/// - `mid`: The index where the array is split into two halves.
/// Sorts a slice using the Quick Sort algorithm.
///
/// Quick Sort is a divide-and-conquer algorithm that picks a 'pivot' element from the array,
/// partitions the array around the pivot, and recursively sorts the two subarrays.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// # Time Complexity
/// - Best: `O(n log n)`
/// - Worst: `O(n²)`
/// - Average: `O(n log n)`
///
/// # Space Complexity
/// - `O(log n)` (due to recursion stack)
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::quick_sort;
///
/// let mut arr = [5, 3, 8, 4, 2];
/// quick_sort(&mut arr);
/// assert_eq!(arr, [2, 3, 4, 5, 8]);
/// ```
/// Partitions the slice around a pivot element.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice to be partitioned.
///
/// # Returns
/// - The index of the pivot element after partitioning.
/// Sorts a slice using the Counting Sort algorithm.
///
/// Counting sort works by counting the occurrences of each element and then reconstructing the sorted
/// array based on the counts.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// # Time Complexity
/// - Best: `O(n + k)`
/// - Worst: `O(n + k)`
/// - Average: `O(n + k)`
///
/// # Space Complexity
/// - `O(k)` (requires extra space for the count array)
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::counting_sort;
///
/// let mut arr = [5, 3, 8, 4, 2];
/// counting_sort(&mut arr);
/// assert_eq!(arr, [2, 3, 4, 5, 8]);
/// ```
/// Sorts a slice using the Radix Sort algorithm.
///
/// Radix sort processes the elements digit by digit, sorting them by each digit starting from the least significant digit.
///
/// # Parameters
/// - `arr`: A mutable reference to the slice of integers to be sorted.
///
/// # Time Complexity
/// - Best: `O(nk)`
/// - Worst: `O(nk)`
/// - Average: `O(nk)`
///
/// # Space Complexity
/// - `O(n + k)` (requires additional space for the buckets)
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::radix_sort;
///
/// let mut arr = [170, 45, 75, 90, 802, 24, 2, 66];
/// radix_sort(&mut arr);
/// assert_eq!(arr, [2, 24, 45, 66, 75, 90, 170, 802]);
/// ```
/// Sorts a heap in ascending order (min heap) or descending order (max heap)
///
/// Heap sort transforms its elements into a heap, then repeatedly removes
/// the largest (or smallest) element and places it in the correct position,
/// restoring the heap property after each removal.
///
/// # Parameters
/// - `&mut Heap<i32>`: a mutable reference to a `Heap` instance and sorts its elements
///
/// If the heap is configured as a max-heap, the elements
/// will be sorted in ascending order. If it's a min-heap, the elements
/// will be sorted in descending order.
///
/// # Time Complexity
/// - Best: `O(n log n)`
/// - Worst: `O(n log n)`
/// - Average: `O(n log n)`
///
/// # Space Complexity
/// - `O(n + k)`
///
/// # Examples
/// ```
/// use dsa::algorithms::sorting::heap_sort;
/// use dsa::data_structures::heap::Heap;
///
/// let mut heap = Heap::new(false); // Min-heap
/// heap.values = vec![3, 1, 6, 5, 2, 4];
/// heap_sort(&mut heap);
/// assert_eq!(heap.values, vec![6, 5, 4, 3, 2, 1]);
/// ```