How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Ada 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 Ada programming language
Source code in the ada programming language
type Data_Array is array(Natural range <>) of Integer;
procedure Insertion_Sort(Item : in out Data_Array) is
First : Natural := Item'First;
Last : Natural := Item'Last;
Value : Integer;
J : Integer;
begin
for I in (First + 1)..Last loop
Value := Item(I);
J := I - 1;
while J in Item'range and then Item(J) > Value loop
Item(J + 1) := Item(J);
J := J - 1;
end loop;
Item(J + 1) := Value;
end loop;
end Insertion_Sort;
You may also check:How to resolve the algorithm Monads/Maybe monad step by step in the Java programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Ring programming language
You may also check:How to resolve the algorithm XML/Output step by step in the Erlang programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Go programming language