

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement insertion sort in JavaScript?
Insertion Sort
It is a very simple comparison sort to sort an array. A comparison sort compares the current value that we are trying to sort with other values in the array. It works with one item at a time and iteratively places each item in correct place so as to get a required sorted array.
Actually, insertion sort is not as efficient as some advanced algorithms such as heap sort or merge sort. It is not a best option while dealing with large programs. Because of its low hidden constant value, an insertion sort outperforms some of the advanced algorithms such as heap or quick sort while dealing with small arrays.
Insertion sort works by moving from left to right over an array. It will use the current item as a 'key' and searches for a value to the left of that key to find a location where the key should actually resides.
Algorithm
In the following example the array given is 0,-3,5,8,2,7,6
- Iteration 0 - in the first iteration we only have the actual unsorted array that is 0,-3,5,8,2,7,6.
- iteration 1 - in this iteration the key is a value at index 1 that is -3. The insertion sort compares the key with the values to the left of that key and proceeds the process. since -3 is less than 0, it moves to the left of 0 there by giving the array as -3,0,5,8,2,7,6.
- Iteration 2 - Here the key is 5(value at index 2). It will be compared with the values to its left that is -3 and 0, and placed in its sorted value. So the array after iteration 2 is -3,0,5,8,2,7,6.
- Iteration 3 - In this iteration the key value 8 will be compared with the elements to its left and the final array will be -3,0,5,8,2,7,6.
- Iteration 4 - In this iteration the key value 2 is compared with the values to its left and is placed in its sorted position. So the final array after iteration 4 is -3,0,2,5,8,7,6.
In the same way, at the end of final iteration the sorted array will be -3,0,2,5,6,7,8
Example
<html> <head> <script> function iSort(array) { for (var p = 1; p < array.length; p++) { if (array[p] < array[0]){ array.unshift(array.splice(p,1)[0]); } else if (array[p] > array[p-1]){ continue; } else { for (var q = 1; q < p; q++) { if (array[p] > array[q-1] && array[p] < array[q]){ array.splice(q,0,array.splice(p,1)[0]); } } } } return array; } document.write(iSort([0,-3,5,8,2,7,6])); </script> </body> </html>
Output
-3,0,2,5,6,7,8
- Related Questions & Answers
- Java program to implement insertion sort
- C++ Program to Implement Insertion Sort
- Insertion Sort
- How to implement merge sort in JavaScript?
- How to implement quick sort in JavaScript?
- Insertion sort in Java.
- Insertion Sort in C#
- Implementing insertion sort to sort array of numbers in increasing order using JavaScript
- Insertion Sort List C++
- Code to implement bubble sort - JavaScript
- Insertion Sort in Python Program
- Binary Insertion Sort in C++
- Insertion Sort List in C++
- Program to implement Bucket Sort in JavaScript
- Difference Between Insertion Sort and Selection Sort