How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Dart programming language
How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Dart 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 Dart programming language
Source code in the dart programming language
void main() {
List<int> a = shellSort([1100, 2, 56, 200, -52, 3, 99, 33, 177, -199]);
print('$a');
}
shellSort(List<int> array) {
int n = array.length;
// Start with a big gap, then reduce the gap
for (int gap = n~/2; gap > 0; gap ~/= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already
// in gapped order keep adding one more element
// until the entire array is gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap
// sorted save a[i] in temp and make a hole at
// position i
int temp = array[i];
// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
int j;
for (j = i; j >= gap && array[j - gap] > temp; j -= gap)
array[j] = array[j - gap];
// put temp (the original a[i]) in its correct
// location
array[j] = temp;
}
}
return array;
}
You may also check:How to resolve the algorithm Find the missing permutation step by step in the Aime programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm RCRPG step by step in the Clojure programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the Perl programming language
You may also check:How to resolve the algorithm Matrix chain multiplication step by step in the Nim programming language