How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Wren programming language

Table of Contents

Problem Statement

Show the   Stooge Sort   for an array of integers.

The Stooge Sort algorithm is as follows:

Let's start with the solution:

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

Source code in the wren programming language

var stoogeSort // recursive
stoogeSort = Fn.new { |a, i, j|
    if (a[j] < a[i]) {
        var t = a[i]
        a[i] = a[j]
        a[j] = t
    }
    if (j - i > 1) {
        var t = ((j - i + 1)/3).floor
        stoogeSort.call(a, i, j - t)
        stoogeSort.call(a, i + t, j)
        stoogeSort.call(a, i, j - t)
    }
}

var as = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in as) {
    System.print("Before: %(a)")
    stoogeSort.call(a, 0, a.count-1)
    System.print("After : %(a)")
    System.print()
}

  

You may also check:How to resolve the algorithm Number names step by step in the Racket programming language
You may also check:How to resolve the algorithm Here document step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Date manipulation step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the COBOL programming language