How to resolve the algorithm Sorting algorithms/Strand sort step by step in the Euphoria programming language

Published on 12 May 2024 09:40 PM

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

Source code in the euphoria programming language

function merge(sequence left, sequence right)
    sequence result
    result = {}
    while length(left) > 0 and length(right) > 0 do
        if left[$] <= right[1] then
            exit
        elsif right[$] <= left[1] then
            return result & right & left
        elsif left[1] < right[1] then
            result = append(result,left[1])
            left = left[2..$]
        else
            result = append(result,right[1])
            right = right[2..$]
        end if
    end while
    return result & left & right
end function

function strand_sort(sequence s)
    integer j
    sequence result
    result = {}
    while length(s) > 0 do
        j = length(s)
        for i = 1 to length(s)-1 do
            if s[i] > s[i+1] then
                j = i
                exit
            end if
        end for
        
        result = merge(result,s[1..j])
        s = s[j+1..$]
    end while
    return result
end function

constant s = rand(repeat(1000,10))
puts(1,"Before: ")
? s
puts(1,"After:  ")
? strand_sort(s)

  

You may also check:How to resolve the algorithm Character codes step by step in the Ursa programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Odin programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Scheme programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Delphi/Pascal programming language