How to resolve the algorithm Dutch national flag problem step by step in the VBScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Dutch national flag problem step by step in the VBScript programming language
Table of Contents
Problem Statement
The Dutch national flag is composed of three coloured bands in the order:
The problem posed by Edsger Dijkstra is: When the problem was first posed, Dijkstra then went on to successively refine a solution, minimising the number of swaps and the number of times the colour of a ball needed to determined and restricting the balls to end in an array, ...
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dutch national flag problem step by step in the VBScript programming language
Source code in the vbscript programming language
'Solution derived from http://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/.
'build an unsorted array with n elements
Function build_unsort(n)
flag = Array("red","white","blue")
Set random = CreateObject("System.Random")
Dim arr()
ReDim arr(n)
For i = 0 To n
arr(i) = flag(random.Next_2(0,3))
Next
build_unsort = arr
End Function
'sort routine
Function sort(arr)
lo = 0
mi = 0
hi = UBound(arr)
Do While mi <= hi
Select Case arr(mi)
Case "red"
tmp = arr(lo)
arr(lo) = arr(mi)
arr(mi) = tmp
lo = lo + 1
mi = mi + 1
Case "white"
mi = mi + 1
Case "blue"
tmp = arr(mi)
arr(mi) = arr(hi)
arr(hi) = tmp
hi = hi - 1
End Select
Loop
sort = Join(arr,",")
End Function
unsort = build_unsort(11)
WScript.StdOut.Write "Unsorted: " & Join(unsort,",")
WScript.StdOut.WriteLine
WScript.StdOut.Write "Sorted: " & sort(unsort)
WScript.StdOut.WriteLine
You may also check:How to resolve the algorithm Metallic ratios step by step in the Groovy programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Nth root step by step in the PHP programming language
You may also check:How to resolve the algorithm Variables step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Variable declaration reset step by step in the Nim programming language