How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the XPL0 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 XPL0 programming language

Source code in the xpl0 programming language

code ChOut=8, IntOut=11;

proc InsertionSort(A, L);       \Sort array A of length L
int  A, L;
int  I, J, V;
[for I:= 1 to L-1 do
    [V:= A(I); 
    J:= I-1;
    while J>=0 and A(J)>V do
        [A(J+1):= A(J);
        J:= J-1;
        ];
    A(J+1):= V;
    ];
];

int A, I;
[A:= [3, 1, 4, 1, -5, 9, 2, 6, 5, 4];
InsertionSort(A, 10);
for I:= 0 to 10-1 do [IntOut(0, A(I));  ChOut(0, ^ )];
]

  

You may also check:How to resolve the algorithm Periodic table step by step in the C programming language
You may also check:How to resolve the algorithm Additive primes step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Universal Turing machine step by step in the Python programming language
You may also check:How to resolve the algorithm Character codes step by step in the Dc programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Euphoria programming language