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));
        }
    }
}









Ada: with Ada.Text_IO, Ada.Numerics.Discrete_Random, Ada.Command_Line;

procedure Dutch_National_Flag is

   type Colour_Type is (Red, White, Blue);

   Number: Positive range 2 .. Positive'Last :=
     Positive'Value(Ada.Command_Line.Argument(1));
   

   type Balls is array(1 .. Number) of Colour_Type;

   function Is_Sorted(B: Balls) return Boolean is
      
   begin
      for I in Balls'First .. Balls'Last-1 loop
         if B(I) > B(I+1) then
            return False;
         end if;
      end loop;
      return True;
   end Is_Sorted;

   function Random_Balls return Balls is
      
      package Random_Colour is new Ada.Numerics.Discrete_Random(Colour_Type);
      Gen: Random_Colour.Generator;
      B: Balls;
   begin
      Random_Colour.Reset(Gen);
      loop
         for I in Balls'Range loop
            B(I) := Random_Colour.Random(Gen);
         end loop;
         exit when (not Is_Sorted(B));
         
      end loop;
      return B;
   end Random_Balls;

   procedure Print(Message: String; B: Balls) is
   begin
      Ada.Text_IO.Put(Message);
      for I in B'Range loop
         Ada.Text_IO.Put(Colour_Type'Image(B(I)));
         if I < B'Last then
            Ada.Text_IO.Put(", ");
         else
            Ada.Text_IO.New_Line;
         end if;
      end loop;
   end Print;

   procedure Sort(Bls: in out Balls) is
      

      Cnt: array(Colour_Type) of Natural := (Red => 0, White => 0, Blue => 0);
      Col: Colour_Type;

      procedure Move_Colour_To_Top(Bls: in out Balls;
                                   Colour: Colour_Type;
                                   Start: Positive;
                                   Count: Natural) is
         This: Positive := Start;
         Tmp: Colour_Type;
      begin
         for N in Start .. Start+Count-1 loop
            while Bls(This) /= Colour loop
               This := This + 1;
            end loop; 
            Tmp := Bls(N); Bls(N) := Bls(This); Bls(This) := Tmp; 
            This := This + 1;
         end loop;
      end  Move_Colour_To_Top;

   begin
      for Ball in Balls'Range loop
         
         Col := Bls(Ball);
         Cnt(Col) := Cnt(Col) + 1;
      end loop;
      Move_Colour_To_Top(Bls, Red,   Start => 1,          Count => Cnt(Red));
      Move_Colour_To_Top(Bls, White, Start => 1+Cnt(Red), Count => Cnt(White));
      
   end Sort;

   A: Balls := Random_Balls;

begin
   Print("Original Order: ", A);

   pragma Assert(not Is_Sorted(A));   

   Sort(A); 

   pragma Assert(Is_Sorted(A));   

   Print("After Sorting:  ", A);
end Dutch_National_Flag;