public static int Partition(int[] array, string arrayName, int start, int end, int pivotId, AlgorithmsAnimation animation) { animation.PushFunctionStack($"Partition"); animation.DeclareVariable("start"); animation.DeclareVariable("end"); animation.DeclareVariable("pivotId"); animation.SetVariable("start", start.ToString()); animation.SetVariable("end", end.ToString()); animation.SetVariable("pivotId", pivotId.ToString()); int x = array[pivotId]; animation.DeclareVariable("pivot"); animation.SetVariable("pivot", x.ToString()); Swap(array, arrayName, pivotId, end, animation); int i = start; animation.DeclareVariable("i"); animation.SetVariable("i", i.ToString()); animation.DeclareVariable("j"); for (int j = start; j <= end; j++) { animation.SetVariable("j", j.ToString()); if (array[j] < x) { Swap(array, arrayName, i++, j, animation); animation.SetVariable("i", i.ToString()); } } Swap(array, arrayName, i, end, animation); animation.PopFunctionStack(); return(i); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); arial = Content.Load <SpriteFont>("arial"); whitePixel = Content.Load <Texture2D>("WhitePixel"); world.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); animation = new AlgorithmsAnimation(world, new Texture2DRectangle(whitePixel, new Rectangle(1, 1, 1, 1), Color.Gray), arial); int[] array = new int[] { 100, 20, 30, 50, 90, 0, 102 }; //animation.PushFunctionStack("main"); //animation.DeclareVariable("res"); //animation.CreateArray("g_A", array.Length, 0); //for (int i = 0; i < array.Length; i++) // animation.SetArrayValue("g_A", i, array[i]); //int res = AlgorithmsTesting.LasVegasRandomizedSelect(array, "g_A", 0, array.Length - 1, 2, animation); //animation.SetVariable("res", res); animation.PushFunctionStack("main"); AnimationLanguage lang = new AnimationLanguage(animation); lang.LoadCode(File.ReadAllText("algorithm.txt")); lang.Compile()(); //animation.PopFunctionStack(); }
public static void Swap <T>(T[] array, string arrayName, int i, int j, AlgorithmsAnimation animation) { animation.PushFunctionStack($"ArraySwap({arrayName}, {i}, {j})"); animation.SwapArrayValues(arrayName, i, j); T temp = array[i]; array[i] = array[j]; array[j] = temp; animation.PopFunctionStack(); }
public static int LasVegasRandomizedSelect(int[] array, string arrayName, int start, int end, int i, AlgorithmsAnimation animation) { animation.PushFunctionStack("Select"); animation.DeclareVariable("start"); animation.DeclareVariable("end"); animation.DeclareVariable("i"); animation.SetVariable("start", start.ToString()); animation.SetVariable("end", end.ToString()); animation.SetVariable("i", i.ToString()); if (start == end) { animation.PopFunctionStack(); return(array[start]); } int pivot = Partition(array, arrayName, start, end, rand.Next(start, end + 1), animation); animation.DeclareVariable("pivot"); animation.SetVariable("pivot", pivot.ToString()); int k = pivot - start + 1; animation.DeclareVariable("k"); animation.SetVariable("k", k.ToString()); int res; if (k == i) { res = array[pivot]; } else if (i < k) { res = LasVegasRandomizedSelect(array, arrayName, start, pivot - 1, i, animation); } else { res = LasVegasRandomizedSelect(array, arrayName, pivot + 1, end, i - k, animation); } animation.PopFunctionStack(); return(res); }