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

Published on 12 May 2024 09:40 PM

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

Source code in the sather programming language

class SORT{T < $IS_LT{T}} is

  private swap(inout a, inout b:T) is
    temp ::= a;
    a := b;
    b := temp;
  end;

-- ---------------------------------------------------------------------------------
  comb_sort(inout a:ARRAY{T}) is
    gap ::= a.size;
    swapped ::= true;
    loop until!(gap <= 1 and ~swapped);
      if gap > 1 then
        gap := (gap.flt / 1.25).int;
      end;
      i ::= 0;
      swapped := false;
      loop until! ( (i + gap) >= a.size );
        if (a[i] > a[i+gap]) then
	  swap(inout a[i], inout a[i+gap]);
	  swapped := true;
	end;
        i := i + 1;
      end;
    end;
  end;
end;

class MAIN is
  main is
    a:ARRAY{INT} := |88, 18, 31, 44, 4, 0, 8, 81, 14, 78, 20, 76, 84, 33, 73, 75, 82, 5, 62, 70|;
    b ::= a.copy;
    SORT{INT}::comb_sort(inout b);
    #OUT + b + "\n";
  end;
end;

  

You may also check:How to resolve the algorithm Cyclops numbers step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Ecstasy programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Sidef programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the SparForte programming language
You may also check:How to resolve the algorithm Secure temporary file step by step in the TUSCRIPT programming language