January 2018
Intermediate to advanced
332 pages
7h 36m
English
Based on the preceding pseudo-code described, implementing Insertionsort is very easy. Let's first create a folder called sort and then create a file called insertion.js in which we will add our insertion class, as shown in the following code:
class Insertion { sort(data) { // loop over all the entries excluding the first record for (var i = 1; i< data.length; ++i) { // take each entry var current = data[i]; // previous entry var j = i-1; // until beginning or until previous data is lesser than current while (j >= 0 && data[j].pages < current.pages) { // shift entries to right data[j + 1] = data[j]; // decrement position for next iteration j = j - 1; } // push current data to new position data[j+1] = current ...
Read now
Unlock full access