avatarShingo K

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2346

Abstract

onstructor</span>(array) { //set an<span class="hljs-built_in"> array </span> }</pre></div><div id="4ba5"><pre> <span class="hljs-comment">//h-sorted</span> <span class="hljs-title function_">sort</span>(<span class="hljs-params">h</span>) { <span class="hljs-comment">//Start with a big gap, then reduce the gap</span> <span class="hljs-comment">//Set a gap with a value of h </span> <span class="hljs-comment">// Do an insertion sort for this gap size (h) until h is greater than 0</span> <span class="hljs-comment">// The first elements array[0...h-1] are already in order</span> <span class="hljs-comment">// Keep adding one more element until the entire array is sorted</span></pre></div><div id="852b"><pre> // <span class="hljs-keyword">Add</span> <span class="hljs-keyword">array</span>[i] <span class="hljs-keyword">to</span> the elements that have been sorted // Save <span class="hljs-keyword">array</span>[i] <span class="hljs-keyword">in</span> <span class="hljs-keyword">temp</span> // tmp = <span class="hljs-keyword">array</span>[i]</pre></div><div id="6657"><pre> // Shift earlier sorted elements up <span class="hljs-keyword">until</span> the correct // <span class="hljs-keyword">location</span> <span class="hljs-keyword">for</span> <span class="hljs-keyword">array</span>[i] <span class="hljs-keyword">is</span> <span class="hljs-built_in">found</span></pre></div><div id="4124"><pre> // Put <span class="hljs-keyword">temp</span> (the original <span class="hljs-keyword">array</span>[i]) <span class="hljs-keyword">in</span> its correct <span class="hljs-keyword">location</span> // <span class="hljs-keyword">array</span>[i] = tmp</pre></div><div id="26bd"><pre> // Return sorted<span class="hljs-built_in"> array </span> } }</pre></div><h2 id="6da8">Full code</h2><p id="25d2">Here is a full code:</p><div id="7acd"><pre><span class="hljs-keyword">class</span> <span class="hljs-symbol">ShellSort</span> { constructor(<span class="hljs-built_in">array</span>) { <span class="hljs-keyword">this</span>.<span class="hljs-built_in">array</span> = <span class="hljs-built_in">array</span>; }</pre></div><div id="cd6f"><pre> <span class="hljs-title function_">sort</span>(<span class="hljs-params">h</span>) { <span class="hljs-keyword

Options

">let</span> gap = h;</pre></div><div id="6f62"><pre> <span class="hljs-keyword">while</span> (gap > <span class="hljs-number">0</span>){ <span class="hljs-keyword">for</span> (<span class="hljs-built_in">let</span> i = <span class="hljs-number">0</span>; i < this.<span class="hljs-built_in">array</span>.<span class="hljs-built_in">length</span>; i += <span class="hljs-number">1</span>){ <span class="hljs-built_in">let</span> unsortedIndex = i;</pre></div><div id="d7fe"><pre> let tmp = <span class="hljs-keyword">this</span>.<span class="hljs-built_in">array</span>[i]; <span class="hljs-keyword">while</span> ((unsortedIndex >= gap) && (<span class="hljs-keyword">this</span>.<span class="hljs-built_in">array</span>[unsortedIndex - gap] > tmp)){ <span class="hljs-keyword">this</span>.<span class="hljs-built_in">array</span>[unsortedIndex] = <span class="hljs-keyword">this</span>.<span class="hljs-built_in">array</span>[unsortedIndex - gap]; unsortedIndex = unsortedIndex - gap; } <span class="hljs-keyword">this</span>.<span class="hljs-built_in">array</span>[unsortedIndex] = tmp; }</pre></div><div id="3d7b"><pre> <span class="hljs-keyword">if</span> (Math.<span class="hljs-built_in">floor</span>(gap/<span class="hljs-number">2</span>) !== <span class="hljs-number">0</span>) { gap = Math.<span class="hljs-built_in">floor</span>(gap/<span class="hljs-number">2</span>); } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (gap === <span class="hljs-number">1</span>) { gap = <span class="hljs-number">0</span>; } <span class="hljs-keyword">else</span> { gap = <span class="hljs-number">1</span>; } } <span class="hljs-keyword">return</span> this.<span class="hljs-built_in">array</span>; } }</pre></div><h2 id="8f99">Resources</h2><ul><li><a href="https://www.geeksforgeeks.org/shellsort/">https://www.geeksforgeeks.org/shellsort/</a></li><li><a href="http://interactivepython.org/runestone/static/pythonds/SortSearch/TheShellSort.html">http://interactivepython.org/runestone/static/pythonds/SortSearch/TheShellSort.html</a></li><li><a href="https://en.wikipedia.org/wiki/Shellsort">https://en.wikipedia.org/wiki/Shellsort</a></li></ul></article></body>

Sorting algorithms Pt1. Shellsort

Now I’m learning sorting algorithms with JavaScript so I wrote about the results and my thoughts on it. In this blog post, I picked up “Shellsort” for the part 1.

About Shellsort

Shellsort is a variation of insertion sort. This sorting algorithm is devised by Donald L. Shell in 1959. That’s why the sorting algorithm is called “Shellsort”. It’s sometimes called “diminishing increment sort” as well. It improves on the insertion sort by breaking the original list into a number of smaller sublists. Each sublist is sorted using the insertion sort.

Well, I’ll explain about the flow of Shellsort.

Firstly, sublists are created. The shell sort uses a “gap” h, sometimes called the “increment”. The sublist is made by choosing all items that are h items apart. Then, each sublist is sorted by the insertion sort, which is called h-sorted.

Although this list is not completely sorted, by h-sorted, the items have been moved closer to where these items actually belong. It means a whole list can become partially sorted.

Next, the value of h is reduced and then sublists are created, then these sublists are sorted. In Shellsort, we have to repeat this cycle until h becomes 1. Finally, when h is 1, the list is sorted by using this normal insertion sort.

Why Shellsort is better than insertion sort?

It is said that the insertion sort is generally effective in “roughly sorted” list (items), which means the list is not completely sorted but sorted to some extent. The insertion sort moves elements only one position ahead. When an element has to be moved far ahead, many movements are involved. That’s why the sorting speed is slow. The Shellsort allows the swap of items located far away. In the Sellsort, when the value of h become 1, the list can be roughly sorted. It means the sorting speed is faster.

Pseudocode

Before starting actual code, here’s a Pseudocode.

class ShellSort {
  constructor(array) {
    //set an array
  }
  //h-sorted
  sort(h) {
    //Start with a big gap, then reduce the gap
    //Set a gap with a value of h 
    // Do an insertion sort for this gap size (h) until h is greater than 0
    // The first elements array[0...h-1] are already in order
    // Keep adding one more element until the entire array is sorted
      // Add array[i] to the elements that have been sorted
      // Save array[i] in temp
        // tmp = array[i]
      // Shift earlier sorted elements up until the correct
      // location for array[i] is found
        // Put temp (the original array[i]) in its correct location
          // array[i] = tmp
    // Return sorted array
  }
}

Full code

Here is a full code:

class ShellSort {
  constructor(array) {
    this.array = array;
  }
  sort(h) {
    let gap = h;
    while (gap > 0){
      for (let i = 0; i < this.array.length; i += 1){
        let unsortedIndex = i;
        let tmp = this.array[i];
        while ((unsortedIndex >= gap) && (this.array[unsortedIndex - gap] > tmp)){
          this.array[unsortedIndex] = this.array[unsortedIndex - gap];
          unsortedIndex = unsortedIndex - gap;
        }
        this.array[unsortedIndex] = tmp;
      }
      if (Math.floor(gap/2) !== 0) {
        gap = Math.floor(gap/2);
      } else if (gap === 1) {
        gap = 0;
      } else {
        gap = 1;
      }
    }
    return this.array;
  }
}

Resources

JavaScript
Recommended from ReadMedium