How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the zkl programming language
Table of Contents
Problem Statement
An O(n2) sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part. Although insertion sort is an O(n2) algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases:
The algorithm is as follows (from wikipedia): Writing the algorithm for integers will suffice.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the zkl programming language
Source code in the zkl programming language
fcn insertionSort(list){
sink:=List();
foreach x in (list){
if(False==(n:=sink.filter1n('>(x)))) sink.append(x); // x>all items in sink
else sink.insert(n,x);
}
sink.close();
}
insertionSort(T(4,65,2,-31,0,99,2,83,782,1)).println();
insertionSort("big fjords vex quick waltz nymph".split()).println();
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Order programming language
You may also check:How to resolve the algorithm MD5 step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the X86 Assembly programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Lua programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Forth programming language