January 2018
Intermediate to advanced
332 pages
7h 36m
English
The Lomuto Partition Scheme is very similar to the simple sort function that we implemented earlier. The difference is that once we select the last element as the pivot, we need to keep adjusting its position by sorting and swapping the elements in memory, as shown in the following code:
partitionLomuto(data, low, high) { // Take pivot as the high value var pivot = high; // initialize loop pointer variable var i = low; // loop over all values except the last (pivot) for(var j = low; j < high - 1; j++) { // if value greater than pivot if (data[j].pages >= data[pivot].pages) { // swap data this.swap(data, i , j); // increment pointer i++; } } // final swap to place pivot at correct // position by swapping this.swap
Read now
Unlock full access