How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Seed7 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Seed7 programming language
Table of Contents
Problem Statement
Sort an array (or list) of elements using the Selection sort algorithm.
It works as follows: First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
Its asymptotic complexity is O(n2) making it inefficient on large arrays. Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM. No other sorting algorithm has less data movement.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Seed7 programming language
Source code in the seed7 programming language
const proc: selectionSort (inout array elemType: arr) is func
local
var integer: i is 0;
var integer: j is 0;
var integer: min is 0;
var elemType: help is elemType.value;
begin
for i range 1 to length(arr) - 1 do
min := i;
for j range i + 1 to length(arr) do
if arr[j] < arr[min] then
min := j;
end if;
end for;
help := arr[min];
arr[min] := arr[i];
arr[i] := help;
end for;
end func;
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Phix programming language
You may also check:How to resolve the algorithm Box the compass step by step in the C# programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the Maxima programming language
You may also check:How to resolve the algorithm Logical operations step by step in the FALSE programming language
You may also check:How to resolve the algorithm Mertens function step by step in the Dyalect programming language