Task: Dutch national flag problem



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RosettaCode
{
    class Program
    {
        static void QuickSort(IComparable[] elements, int left, int right)
        {
            int i = left, j = right;
            IComparable pivot = elements[left + (right - left) / 2];

            while (i <= j)
            {
                while (elements[i].CompareTo(pivot) < 0) i++;
                while (elements[j].CompareTo(pivot) > 0) j--;

                if (i <= j)
                {
                    
                    IComparable tmp = elements[i];
                    elements[i] = elements[j];
                    elements[j] = tmp;
                    i++;
                    j--;
                }
            }

            
            if (left < j) QuickSort(elements, left, j);
            if (i < right) QuickSort(elements, i, right);
        }
        const int NUMBALLS = 5;
        static void Main(string[] args)
        {
            Func<string[], bool> IsSorted = (ballList) =>
                {
                    int state = 0;
                    for (int i = 0; i < NUMBALLS; i++)
                    {
                        if (int.Parse(ballList[i]) < state)
                            return false;
                        if (int.Parse(ballList[i]) > state)
                            state = int.Parse(ballList[i]);
                    }
                    return true;
                };
            Func<string[], string> PrintOut = (ballList2) =>
                {
                    StringBuilder str = new StringBuilder();
                    for (int i = 0; i < NUMBALLS; i++)
                        str.Append(int.Parse(ballList2[i]) == 0 ? "r" : int.Parse(ballList2[i]) == 1 ? "w" : "b");
                    return str.ToString();
                };
            bool continueLoop = true;
            string[] balls = new string[NUMBALLS]; 
            Random numberGenerator = new Random();
            do 
            {
                
                for (int i = 0; i < NUMBALLS; i++)
                    balls[i] = numberGenerator.Next(3).ToString();

                continueLoop = IsSorted(balls);
                if (continueLoop)
                    Console.WriteLine("Accidentally still sorted: {0}", PrintOut(balls));
            } while (continueLoop);
            Console.WriteLine("Non-sorted: {0}", PrintOut(balls));
            QuickSort(balls, 0, NUMBALLS - 1); 
            Console.WriteLine("{0}: {1}", IsSorted(balls) ? "Sorted" : "Sort failed", PrintOut(balls));
        }
    }
}






VB: 


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


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