An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
July 13, 2021 04:40 pm GMT
Original Link: https://dev.to/cleancodestudio/insertion-sort-18g7
Insertion Sort (JS Example)
Note: This is not a "professionally written" post. This is a post sharing personal notes I wrote down while preparing for FAANG interviews.
See all of my Google, Amazon, & Facebook interview study notes
Insertion Sort Breakdown
- Worst Complexity: n^2
- Average Complexity: n^2
- Best Complexity: n
- Space Complexity: 1
- Method: Insertion
- Stable: Yes
Insertion Sort Notes
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Wikipedia
Insertion Sort JavaScript Implementation
const InsertionSort = (items = []) => { for (let i = 1; i < items.length; i++) { let index = i-1 let temporary = items[i] while (index >= 0 && items[index] > temporary) { items[index + 1] = items[index] index-- } items[index + 1] = temporary } return items}module.exports = InsertionSort
Original Link: https://dev.to/cleancodestudio/insertion-sort-18g7
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To