How to resolve the algorithm Knuth shuffle step by step in the Ring programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Knuth shuffle step by step in the Ring programming language

Table of Contents

Problem Statement

The   Knuth shuffle   (a.k.a. the Fisher-Yates shuffle)   is an algorithm for randomly shuffling the elements of an array.

Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).

Given an array items with indices ranging from 0 to last, the algorithm can be defined as follows (pseudo-code):

(These are listed here just for your convenience; no need to demonstrate them on the page.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Knuth shuffle step by step in the Ring programming language

Source code in the ring programming language

# Project : Knuth shuffle

items = list(52)
for n = 1 to len(items) 
      items[n] = n
next
knuth(items)
showarray(items)

func knuth(items)
       for i = len(items) to 1 step -1
            j = random(i-1) + 1 
            if i != j
               temp = items[i]
               items[i] = items[j]
               items[j] = temp
            ok
       next

func showarray(vect)
       see "["
       svect = ""
       for n = 1 to len(vect)
           svect = svect + vect[n] + " "
       next
       svect = left(svect, len(svect) - 1)
       see svect
       see "]" + nl

  

You may also check:How to resolve the algorithm Compiler/code generator step by step in the Task programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the VBA programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Vigenère cipher/Cryptanalysis step by step in the 11l programming language
You may also check:How to resolve the algorithm Paraffins step by step in the Mathematica/Wolfram Language programming language