Shell Sort

Shell Sort

shellSort.rb

Shell sort is an insertion sorting algorithm: A value is stored to a temporary value, then inserted into its appropriate place. One difference between this algorithm and the traditional insertion sort is that a shell sort compares two elements that are separated by a gap of several positions—in essence, making bigger jumps. This slight change results in greater efficiency in the worst-case scenario. Remember, worst-case scenarios are when a list of elements are in complete disarray and cannot be disordered any more than they currently are.

The Code

 require 'benchmark'

 def shell_sort(a)
     i = 0
     j = 0
     size = a.length
     increment = size / 2
     temp = 0

     while increment > 0
         i = increment
         while i<size
             j = i
 temp = a[i]  ...

Get Wicked Cool Ruby Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.