How to resolve the algorithm Sorting algorithms/Strand sort step by step in the CMake programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Strand sort step by step in the CMake programming language
Table of Contents
Problem Statement
Implement the Strand sort. This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Strand sort step by step in the CMake programming language
Source code in the cmake programming language
# strand_sort(<output variable> [<value>...]) sorts a list of integers.
function(strand_sort var)
# Strand sort moves elements from _ARGN_ to _answer_.
set(answer) # answer: a sorted list
while(DEFINED ARGN)
# Split _ARGN_ into two lists, _accept_ and _reject_.
set(accept) # accept: elements in sorted order
set(reject) # reject: all other elements
set(p)
foreach(e ${ARGN})
if(DEFINED p AND p GREATER ${e})
list(APPEND reject ${e})
else()
list(APPEND accept ${e})
set(p ${e})
endif()
endforeach(e)
# Prepare to merge _accept_ into _answer_. First, convert both lists
# into arrays, for better indexing: set(e ${answer${i}}) is faster
# than list(GET answer ${i} e).
set(la 0)
foreach(e ${answer})
math(EXPR la "${la} + 1")
set(answer${la} ${e})
endforeach(e)
set(lb 0)
foreach(e ${accept})
math(EXPR lb "${lb} + 1")
set(accept${lb} ${e})
endforeach(e)
# Merge _accept_ into _answer_.
set(answer)
set(ia 1)
set(ib 1)
while(NOT ia GREATER ${la}) # Iterate elements of _answer_.
set(ea ${answer${ia}})
while(NOT ib GREATER ${lb}) # Take elements from _accept_,
set(eb ${accept${ib}}) # while they are less than
if(eb LESS ${ea}) # next element of _answer_.
list(APPEND answer ${eb})
math(EXPR ib "${ib} + 1")
else()
break()
endif()
endwhile()
list(APPEND answer ${ea}) # Take next from _answer_.
math(EXPR ia "${ia} + 1")
endwhile()
while(NOT ib GREATER ${lb}) # Take rest of _accept_.
list(APPEND answer ${accept${ib}})
math(EXPR ib "${ib} + 1")
endwhile()
# This _reject_ becomes next _ARGN_. If _reject_ is empty, then
# set(ARGN) undefines _ARGN_, breaking the loop.
set(ARGN ${reject})
endwhile(DEFINED ARGN)
set("${var}" ${answer} PARENT_SCOPE)
endfunction(strand_sort)
strand_sort(result 11 55 55 44 11 33 33 44 22 22)
message(STATUS "${result}") # -- 11;11;22;22;33;33;44;44;55;55
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Wren programming language
You may also check:How to resolve the algorithm Environment variables step by step in the EMal programming language
You may also check:How to resolve the algorithm A+B step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the Erlang programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the MiniScript programming language