Skip to content

Commit 4a74260

Browse files
authored
feat: add docs for bubble and insertion sort (alexfertel#44)
1 parent 412a987 commit 4a74260

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/sorting/bubble_sort.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
// It sorts the array by repeatedly comparing the adjacent elements and swapping them if they are in the wrong order
2+
// Time complexity is O(N^2)
3+
// Auxiliary space is O(1)
4+
15
pub fn bubble_sort<T: Ord>(array: &mut [T]) {
26
for i in 0..array.len() {
7+
// Last i elements are already in place
38
for j in 0..array.len() - 1 - i {
49
if array[j] > array[j + 1] {
510
array.swap(j, j + 1);

src/sorting/insertion_sort.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
// Insertion sort divides the array into sorted and unsorted parts.
2+
// Values from the unsorted parts are placed in the correct position in the sorted part
3+
// time complexity is O(N^2)
4+
// Auxiliary space is O(1)
5+
16
pub fn insertion_sort<T: Ord>(array: &mut [T]) {
7+
// initialise the array
28
for i in 0..array.len() {
39
let mut j = i;
10+
// Move elements of arr[0..i-1],
11+
// that are greater than key, to one
12+
// position ahead of their
13+
// current position
414
while j > 0 && array[j] < array[j - 1] {
515
array.swap(j, j - 1);
616
j -= 1;

0 commit comments

Comments
 (0)