How to resolve the algorithm Sorting algorithms/Selection sort step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

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

Source code in the 11l programming language

F selection_sort(&lst)
   L(e) lst
      V mn = min(L.index .< lst.len, key' x -> @lst[x])
      (lst[L.index], lst[mn]) = (lst[mn], e)

V arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
selection_sort(&arr)
print(arr)

  

You may also check:How to resolve the algorithm Unbias a random generator step by step in the F# programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Word frequency step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Array length step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the Scheme programming language