How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Julia programming language
How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Julia 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 Julia programming language
This Julia code implements the insertion sort algorithm, which is an in-place sorting algorithm that builds the final sorted array one item at a time. It works by comparing each element of the array with the elements to its left, and moving it to the correct position.
The function insertionsort!
takes an array A
of type T
as input, where T
is a subtype of Number
. The !
in the function name indicates that the function modifies the input array in-place. The function returns the sorted array.
The function first iterates over the elements of the array from the second element to the last element. For each element A[i+1]
, it compares the element with the elements to its left, and moves it to the correct position.
The variable j
is used to track the position of the element being compared. The while loop continues until j
is greater than 0 and the element at A[j]
is greater than the element being compared. If the element at A[j]
is greater, the element at A[j+1]
is moved to A[j]
, and j
is decremented by 1.
Once the correct position for the element being compared has been found, the element is placed at that position and the loop moves on to the next element in the array.
The following is an example of how to use the insertionsort!
function:
julia> x = randn(5)
5-element Array{Float64,1}:
0.7812555397389255
-0.10585431761395037
0.6808233360452348
-0.531351308078714
-1.780242073794295
julia> insertionsort!(x)
5-element Array{Float64,1}:
-1.780242073794295
-0.531351308078714
-0.10585431761395037
0.6808233360452348
0.7812555397389255
In this example, the insertionsort!
function is applied to an array of five random numbers. The sorted array is returned and displayed.
Source code in the julia programming language
# v0.6
function insertionsort!(A::Array{T}) where T <: Number
for i in 1:length(A)-1
value = A[i+1]
j = i
while j > 0 && A[j] > value
A[j+1] = A[j]
j -= 1
end
A[j+1] = value
end
return A
end
x = randn(5)
@show x insertionsort!(x)
You may also check:How to resolve the algorithm Hello world/Web server step by step in the Perl programming language
You may also check:How to resolve the algorithm Compiler/syntax analyzer step by step in the Python programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Zig programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the MUMPS programming language