How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Elena programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Elena programming language

Table of Contents

Problem Statement

Implement a   comb sort.

The Comb Sort is a variant of the Bubble Sort. Like the Shell sort, the Comb Sort increases the gap used in comparisons and exchanges. Dividing the gap by

( 1 −

e

− φ

)

− 1

≈ 1.247330950103979

{\displaystyle (1-e^{-\varphi })^{-1}\approx 1.247330950103979}

works best, but   1.3   may be more practical.

Some implementations use the insertion sort once the gap is less than a certain amount.

Variants:

Pseudocode:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Elena programming language

Source code in the elena programming language

import extensions;
import system'math;
import system'routines;
 
extension op
{
    combSort()
    {
        var list := self.clone();
 
        real gap := list.Length;
        bool swaps := true;
        while (gap > 1 || swaps)
        {
            gap /= 1.247330950103979r;
            if (gap<1) { gap := 1 };
 
            int i := 0;
            swaps := false;
            while (i + gap.RoundedInt < list.Length)
            {
                int igap := i + gap.RoundedInt;
                if (list[i] > list[igap])
                {
                    list.exchange(i,igap);
                    swaps := true
                };
                i += 1
            }
        };
 
        ^ list        
    }
}
 
public program()
{
    var list := new int[]{3, 5, 1, 9, 7, 6, 8, 2, 4 };
 
    console.printLine("before:", list.asEnumerable());
    console.printLine("after :", list.combSort().asEnumerable())
}

  

You may also check:How to resolve the algorithm Call a function step by step in the Clojure programming language
You may also check:How to resolve the algorithm Environment variables step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Stream merge step by step in the Tcl programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Swift programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the AArch64 Assembly programming language