How to resolve the algorithm Sorting algorithms/Shell sort step by step in the PureBasic programming language
How to resolve the algorithm Sorting algorithms/Shell sort step by step in the PureBasic programming language
Table of Contents
Problem Statement
Sort an array of elements using the Shell sort algorithm, a diminishing increment sort. The Shell sort (also known as Shellsort or Shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959. Shell sort is a sequence of interleaved insertion sorts based on an increment sequence. The increment size is reduced after each pass until the increment size is 1. With an increment size of 1, the sort is a basic insertion sort, but by this time the data is guaranteed to be almost sorted, which is insertion sort's "best case". Any sequence will sort the data as long as it ends in 1, but some work better than others. Empirical studies have shown a geometric increment sequence with a ratio of about 2.2 work well in practice. [1] Other good sequences are found at the On-Line Encyclopedia of Integer Sequences.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Shell sort step by step in the PureBasic programming language
Source code in the purebasic programming language
#STEP=2.2
Procedure Shell_sort(Array A(1))
Protected l=ArraySize(A()), increment=Int(l/#STEP)
Protected i, j, temp
While increment
For i= increment To l
j=i
temp=A(i)
While j>=increment And A(j-increment)>temp
A(j)=A(j-increment)
j-increment
Wend
A(j)=temp
Next i
If increment=2
increment=1
Else
increment*(5.0/11)
EndIf
Wend
EndProcedure
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Dylan programming language
You may also check:How to resolve the algorithm Modified random distribution step by step in the Rust programming language
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Factor programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the BaCon programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Raku programming language