Skip to content

Commit c4f004b

Browse files
committed
Add time complexity info for sort algorithms
- add time complexity info for all sort algorithm (bubble, heap, insertion and selection)
1 parent 08e8e44 commit c4f004b

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

Inc/sort/bubble_sort.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
#include "typedefs.h"
3939

4040
/**
41-
* @brief Sort elements using bubble sort algorithm.
41+
* @brief Sort elements using bubble sort algorithm. It compares the adjacent elements and swaps them
42+
* if they are in the wrong order. It is done repeatedly until all elements are sorted.
43+
* Time complexity: O(N^2)
4244
*
4345
* @param[in/out] *buffer Pointer to the buffer that contains elements that will be sorted.
4446
* @param[in] number_of_elements Number of elements in the buffer.

Inc/sort/heap_sort.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
#include "typedefs.h"
3939

4040
/**
41-
* @brief Sort elements using heap sort algorithm.
41+
* @brief Sort elements using heap sort algorithm. It uses heapify to convert array into max heap.
42+
* It deletes the root node of the max heap (one by one) and replaces it with the last node. After that,
43+
* heapify is done again. Whole process is repeated while heap size is greater than 1.
44+
* Time complexity: O(N log N)
4245
*
4346
* @param[in/out] *buffer Pointer to the buffer that contains elements that will be sorted.
4447
* @param[in] number_of_elements Number of elements in the buffer.

Inc/sort/insertion_sort.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
#define MAX_ELEMENT_SIZE (64)
4141

4242
/**
43-
* @brief Sort elements using insertion sort algorithm. Insertion sort algorithm is a simple
44-
* sorting algorithm that has O(N^2) time complexity. It is efficient on smaller lists, and
45-
* much less efficient on large lists.
43+
* @brief Sort elements using insertion sort algorithm. Divide given list into sorted and unsorted part
44+
* (first element is considered sorted). Iterate through each element in the unsorted part. Pick one
45+
* element at a time, and insert it into correct position in the sorted part. It is efficient on smaller
46+
* lists, and much less efficient on large lists.
47+
* Time complexity: O(N^2)
4648
*
4749
* NOTE: implemented that the max element size is 64 B. If element size is larger, then
4850
* MAX_ELEMENT_SIZE macro must be updated.

Inc/sort/selection_sort.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
#include "typedefs.h"
3939

4040
/**
41-
* @brief Sort elements using selection sort algorithm. Selection sort algorithm is an in-place
42-
* comparison sorting algorithm that has O(N^2) time complexity. It divides the input array into
41+
* @brief Sort elements using selection sort algorithm. It divides the input array into
4342
* two parts, sorted sub-array of items which is built up from left to right and unsorted items
4443
* that occupies the rest of the array. The sorted sub-array is empty and the unsorted sub-array
4544
* is the entire input array. The algorithm proceeds by finding the smallest element in the
4645
* unsorted sub-array, swapping it with the leftmost unsorted element (putting it in sorted order),
4746
* and moving the sub-array boundaries one element to the right.
47+
* Time complexity: O(N^2)
4848
*
4949
* @param[in/out] *buffer Pointer to the buffer that contains elements that will be sorted.
5050
* @param[in] number_of_elements Number of elements in the buffer.

0 commit comments

Comments
 (0)