Shell Sort Algorithm and Code in Javascript
The Shell Sort algorithm is basically extended from or variation of insertion algorithm. In the Insertion algorithm, we used to compare and swap two adjacent values from given input values. But in the Shell Sort algorithm, we select and swap distance values or values that are not adjacent or have more gap than one. This algorithm is basically inherited properties from Insertion algorithm as its last step is insertion algorithm. Shell Sorting algorithm works on gap or distance or interval in between input values. The formula used to find the gap is (gap= n/2), where n is a total number of values that are divided by 2 to get the value of gap. We continue applying this formula until we get gap value 1 that is the last step of this algorithm totally based on Insertion algorithm.Flow Chart of Shell Sort in Javascript

Pseudo code of Shell Sort Algorithm in Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Method sortshell(){ Array = List of input in an array; Variable gap = array.length/2; While(gap>0){ For(Integer i = gap; i is less than array.lenght; increment i ) { Create a temporary variable as container; While(value of i - gap > value of temporary variable ){ Swap values; } } gap = gap/2; } } |





Shell Sort Code in Javascript
Let’s see a small part of the Shell Sort Code for better understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function sorting() { var array = [41, 2, 33, 11, 24]; var gap = arrayy.length / 2; while (gap > 0) { for (int x = gap; x < array.length; x++) { var y = x; var container = array[x]; while (y >= gap && array[y-gap] > container) { array[y] = array[y-gap]; y = y - gap; } array[y] = container; } gap = gap/2; } window.alert(array); } |