How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Haxe programming language
How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Haxe 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 Haxe programming language
Source code in the haxe programming language
class ShellSort {
public static function sort<T>(arr:Array<T>) {
var h = arr.length;
while (h > 0) {
h >>= 1;
for (i in h...arr.length) {
var k = arr[i];
var j = i;
while (j >= h && Reflect.compare(k, arr[j - h]) < 0) {
arr[j] = arr[j - h];
j -= h;
}
arr[j] = k;
}
}
}
}
class Main {
static function main() {
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
3.5, 0.0, -4.1, -9.5];
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
'be', 'self-evident', 'that', 'all',
'men', 'are', 'created', 'equal'];
Sys.println('Unsorted Integers: ' + integerArray);
ShellSort.sort(integerArray);
Sys.println('Sorted Integers: ' + integerArray);
Sys.println('Unsorted Floats: ' + floatArray);
ShellSort.sort(floatArray);
Sys.println('Sorted Floats: ' + floatArray);
Sys.println('Unsorted Strings: ' + stringArray);
ShellSort.sort(stringArray);
Sys.println('Sorted Strings: ' + stringArray);
}
}
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Java programming language
You may also check:How to resolve the algorithm Singleton step by step in the Objeck programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Odin programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Raku programming language
You may also check:How to resolve the algorithm Almost prime step by step in the EasyLang programming language