Skip to content

Commit 53d784f

Browse files
authored
feat(docs): improve sorting algorithms docs (#62)
1 parent 75c9d83 commit 53d784f

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

src/sorting/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,123 @@ __Sources to read:__
120120
* [Geeksforgeeks](https://www.geeksforgeeks.org/selection-sort/)
121121
* [Programiz](https://www.programiz.com/dsa/selection-sort)
122122

123+
### [Heap Sort](./heap_sort.rs)
124+
125+
Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure. The main idea behind heap sort is to turn the array into a max-heap structure, which ensures that the largest element of the heap is located at the root. This largest element is then swapped with the last element and removed from the heap. This process is repeated until the heap is empty, resulting in a sorted array.
126+
127+
* Safety: One of Rust's main features is memory safety. In the heap sort implementation, Rust ensures safety by using borrowing and the slice notation to manipulate parts of the array.
128+
* In-place Swapping: Rust provides an efficient way to swap two elements in an array with arr.swap(i, j), which makes the algorithm elegant and concise.
129+
* Performance: Rust, being a systems programming language, ensures that the heap sort implementation is efficient and can be optimized further by the Rust compiler.
130+
131+
![Alt text](image-4.png)
132+
133+
__Properties__
134+
* In-place sorting: It sorts the data in place, requiring only a constant amount of additional memory.
135+
* Time complexity: Heap sort has a worst-case time complexity of O(n log n).
136+
137+
__Sources to read:__
138+
* [Wikipedia](https://en.wikipedia.org/wiki/Heapsort)
139+
* [Geeksforgeeks](https://www.geeksforgeeks.org/heap-sort/)
140+
* [Programiz](https://www.programiz.com/dsa/heap-sort)
141+
142+
### [Odd-Even Sort](./odd_even_sort.rs)
143+
144+
Odd-Even Sort, also known as Brick Sort, is a relatively simple sorting algorithm, inspired by the idea behind Bubble Sort. The main idea is to repeatedly make two passes on an array: one for every odd-indexed element pair and the other for every even-indexed element pair.
145+
146+
__Algorithm__
147+
* Odd Step: Compare all odd-indexed elements of the array with their next neighbor (i.e., at index i and i+1) and swap them if the element at index i is greater than the one at i+1.
148+
* Even Step: Compare all even-indexed elements of the array with their next neighbor (i.e., at index i and i+1) and swap them if the element at index i is greater than the one at i+1.
149+
* Repeat the two steps until the array is sorted.
150+
151+
![Alt text](image.png)
152+
153+
__Properties__
154+
* Worst case : O(n^2)
155+
* Best case : O(n)
156+
* Average case : O(n^2)
157+
158+
__Sources to read:__
159+
* [Wikipedia](https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort)
160+
* [Geeksforgeeks](https://www.geeksforgeeks.org/odd-even-sort-brick-sort/)
161+
* [Programiz](https://www.programiz.com/dsa/heap-sort)
162+
163+
164+
### [Pancake Sort](./pancake_sort.rs)
165+
166+
The main operation in Pancake Sort is a "flip" operation. Given an integer k, the flip operation reverses the order of the first k elements in the array.
167+
168+
The idea behind Pancake Sort:
169+
170+
* Find the index of the maximum element in the array.
171+
* Use the flip operation to move this maximum element to the beginning of the array.
172+
* Use another flip operation to move this maximum element to its correct position at the end of the array.
173+
* Exclude the last element and repeat the above steps for the rest of the array.
174+
175+
The goal is to move the largest unsorted element to its correct position in each iteration.
176+
177+
![Alt text](image-3.png)
178+
179+
__Properties__
180+
* Worst case : O(n^2)
181+
* Average case : O(n^2)
182+
183+
__Sources to read:__
184+
* [Wikipedia](https://en.wikipedia.org/wiki/Pancake_sorting)
185+
* [Geeksforgeeks](https://www.geeksforgeeks.org/pancake-sorting/)
186+
187+
188+
### [Merge Sort](./merge_sort.rs)
189+
190+
Merge sort is a divide-and-conquer sorting algorithm. It works as follows:
191+
192+
* Divide the unsorted list into n sublists, each containing one element.
193+
* Repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining.
194+
195+
* The merge_sort function first checks if the array has a length of 1 or 0. If so, it's already sorted, so it returns the array as-is.
196+
* The array is split into two halves, which are recursively sorted.
197+
* The sorted halves are merged together using the merge function.
198+
* The merge function takes two sorted arrays (or slices) and returns a new array that contains all the elements from both arrays in sorted order. It does this by iterating through both arrays simultaneously, always choosing the smallest of the two current elements to append to the result.
199+
200+
![Alt text](image-2.png)
201+
202+
__Properties__
203+
* Worst case : O(nlogn)
204+
* Average case : O(nlogn)
205+
* Best case : O(nlogn)
206+
207+
__Sources to read:__
208+
* [Wikipedia](https://en.wikipedia.org/wiki/Merge_sort)
209+
* [Geeksforgeeks](https://www.geeksforgeeks.org/merge-sort/)
210+
* [Programiz](https://www.programiz.com/dsa/merge-sort)
211+
212+
213+
### [Insertion Sort](./insertion_sort.rs)
214+
215+
Insertion sort is a simple and intuitive sorting algorithm that builds the final sorted array one element at a time. It's best suited for small arrays or arrays that are already nearly sorted.
216+
217+
The basic idea of insertion sort is similar to the way many people sort playing cards in their hands. We start from the beginning of the array and continuously insert elements in their correct position in the sorted portion of the array.
218+
219+
Here's a step-by-step explanation followed by a Rust implementation:
220+
221+
* Start from the second element (index 1) assuming the element at index 0 is sorted.
222+
* Compare the current element with the previous elements. If the current element is smaller than the previous element, we keep comparing with the elements before until we reach an element smaller or reach the start of the array.
223+
* Insert the current element in its correct position so that the elements before are all smaller than the current element.
224+
* Repeat the process for each of the elements in the array.
225+
226+
![Alt text](image-1.png)
227+
228+
__Properties__
229+
* Worst case : O(n^2)
230+
* Average case : O(n^2)
231+
* Best case : O(n)
232+
233+
__Space Complexity__
234+
* O(1) - Because it's an in-place sorting algorithm (i.e., it doesn't require any additional storage).
235+
236+
__Sources to read:__
237+
* [Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)
238+
* [Geeksforgeeks](https://www.geeksforgeeks.org/insertion-sort/)
239+
* [Programiz](https://www.programiz.com/dsa/insertion-sort)
240+
123241

124242

src/sorting/image-1.png

10.2 KB
Loading

src/sorting/image-2.png

42.4 KB
Loading

src/sorting/image-3.png

46.1 KB
Loading

src/sorting/image-4.png

48.4 KB
Loading

src/sorting/image.png

10.2 KB
Loading

0 commit comments

Comments
 (0)