locked
Algorithm Simulator

    Question

  • Hey can any one help me to create a simulator for insertion sort ?? it must show the algorithm side with following  array result step by step?
    Hasitha
    Thursday, August 25, 2011 4:45 AM

Answers

  • This example shows a different approach.

    Use animation to show how the recursive solution to the Tower of Hanoi problem works in C#

    It uses a for loop to repeatedly redraw rectangles showing moving objects. You could either draw the values that you are sorting or you could make it move labels instead of drawing.


    Rod Stephens, Visual Basic Programming 24-Hour Trainer (http://www.vb-helper.com/24hourvb.htm)
    • Marked as answer by Hasithahmk Thursday, September 01, 2011 1:46 AM
    • Unmarked as answer by Hasithahmk Thursday, September 01, 2011 1:46 AM
    • Marked as answer by Leo Liu - MSFTModerator Thursday, September 01, 2011 2:17 AM
    Tuesday, August 30, 2011 10:04 PM
  • This is the same question you asked in the C# Language forum (except there it was quicksort).

    The answer is exactlt the same: write code to do the sort, then add lines to output the array or list at appropriate points.

    To forestall further questions: the same answer applies to bubble sort, heap sort, merge sort and any other type of sort. :)


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Thursday, August 25, 2011 11:00 AM
  • 1. I want to display the above algorithm side of my windows form(it can be do using label , but it is too small).Can you suggest a way to that??

    2.then show the array output related to this algoitham  step by step.(How can it do ???)


    Hi Hasithahmk,

    WinForm Label control only has one line, you can turn to use the TextBox control and set the Multiline property of the TextBox control to True, add lines to output the real-time sorting result to the TextBox control using the TextBox.AppendText method.

    You can also use the output methods which belong to the Console class, the action you should perform is to redirect the output container to the TextBox control:

        public class TextBoxStreamWriter : TextWriter
        {
          TextBox txbOutput = null;
    
          public TextBoxStreamWriter(TextBox output)
          {
            this.txbOutput = output;
          }
    
          public override void Write(char value)
          {
            this.txbOutput.AppendText(value.ToString());
          }
    
          public override void Write(string value)
          {
            this.txbOutput.AppendText(value);
          }
    
          public override void WriteLine(string str)
          {
            str += "\r\n";
            this.txbOutput.AppendText(str);
          }
    
          public override Encoding Encoding
          {
            get { return System.Text.Encoding.UTF8; }
          }
        }
    
        public Form1()
        {
          InitializeComponent();
    
          TextBoxStreamWriter customConsole = new TextBoxStreamWriter(textBox1);
          Console.SetOut(customConsole);
    
          Console.WriteLine("Test Start.");
          Console.WriteLine("Test Logic.");
          Console.WriteLine("Test End.");
        }
    


    Have a nice day,


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 30, 2011 5:31 AM
    Moderator

All replies

  • Hello,

    Insertion in what exactly? a linked-list? a simple array? Which algorithm did your teacher ask? Dichotomy?


    Kind regards, Sebastien G. - Axiomtek Deutschland GmbH
    Thursday, August 25, 2011 7:24 AM
  • Simple Array , i haven't any idea how to show step by step happening sorting process ?? 
    Hasitha
    Thursday, August 25, 2011 9:03 AM
  • This is the same question you asked in the C# Language forum (except there it was quicksort).

    The answer is exactlt the same: write code to do the sort, then add lines to output the array or list at appropriate points.

    To forestall further questions: the same answer applies to bubble sort, heap sort, merge sort and any other type of sort. :)


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Thursday, August 25, 2011 11:00 AM
  • Yeah!! i know if one alogoritham is done i can do same as the other.I wrote code for the sorting and its perfectly working, it can get inputs and show the sorted list as output. its ok.

    INSERTION-SORT(A)

    1 for j ¬2 to length[A] 

    2     do key ¬ A[j]

    3          Insert A[j] into the sorted sequence A[1..j-1]

    4           i ¬ j - 1

    5           While i > 0 and A[i] > key

    6                     do  A[i+1] ¬ A[i]

    7                           i ¬i-1

    8            A[i+1] ¬ key

    but my problems are ;

    1. I want to display the above algorithm side of my windows form(it can be do using label , but it is too small).Can you suggest a way to that??

    2.then show the array output related to this algoitham  step by step.(How can it do ???)

    im thakful to u if u can suggest some way to do this !!!!!!!!!


    Hasitha
    Thursday, August 25, 2011 11:16 AM
  • What language is that? It's not C# nor is it VB but has elements of both.

    If you want a C# version try looking at http://www.softwareandfinance.com/CSharp/Insertion_Sort.html


    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Thursday, August 25, 2011 4:54 PM
  • OH ! its a psudoecode ,its not the code , i mean i want only show it in my form ! I know the code for insertion code in C# language !
    Hasitha
    Thursday, August 25, 2011 5:10 PM
  • Problem 1: Have you tried resizing label with mouse or setting the properties to larger values?  Also, a text box may help.

    Problem 2: Add a nested a loop with a write statement inside the code block that inserts data into array.  Each time you add data you automaticly display it as well.

    Good luck.


    David
    Friday, August 26, 2011 1:54 AM
  • 1. I want to display the above algorithm side of my windows form(it can be do using label , but it is too small).Can you suggest a way to that??

    2.then show the array output related to this algoitham  step by step.(How can it do ???)


    Hi Hasithahmk,

    WinForm Label control only has one line, you can turn to use the TextBox control and set the Multiline property of the TextBox control to True, add lines to output the real-time sorting result to the TextBox control using the TextBox.AppendText method.

    You can also use the output methods which belong to the Console class, the action you should perform is to redirect the output container to the TextBox control:

        public class TextBoxStreamWriter : TextWriter
        {
          TextBox txbOutput = null;
    
          public TextBoxStreamWriter(TextBox output)
          {
            this.txbOutput = output;
          }
    
          public override void Write(char value)
          {
            this.txbOutput.AppendText(value.ToString());
          }
    
          public override void Write(string value)
          {
            this.txbOutput.AppendText(value);
          }
    
          public override void WriteLine(string str)
          {
            str += "\r\n";
            this.txbOutput.AppendText(str);
          }
    
          public override Encoding Encoding
          {
            get { return System.Text.Encoding.UTF8; }
          }
        }
    
        public Form1()
        {
          InitializeComponent();
    
          TextBoxStreamWriter customConsole = new TextBoxStreamWriter(textBox1);
          Console.SetOut(customConsole);
    
          Console.WriteLine("Test Start.");
          Console.WriteLine("Test Logic.");
          Console.WriteLine("Test End.");
        }
    


    Have a nice day,


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 30, 2011 5:31 AM
    Moderator
  • I've done things like that where labels moved around to show the values as they were sorted.

    One way to do that is to make a script of the moves you need to make. Basically a big long list holding the object to move and its destination.

    Then you start a timer that loops through the list. It moves the top item a small distance toward its destination--perhaps 10 pixels 10 times per second.

    When an object reaches its destination, remove that entry from the list and start moving the next object.

    After you get that working, you can modify your sorting algorithm to build the script instead of simply sorting the items.


    Rod Stephens, Visual Basic Programming 24-Hour Trainer (http://www.vb-helper.com/24hourvb.htm)
    Tuesday, August 30, 2011 9:49 PM
  • This example shows a different approach.

    Use animation to show how the recursive solution to the Tower of Hanoi problem works in C#

    It uses a for loop to repeatedly redraw rectangles showing moving objects. You could either draw the values that you are sorting or you could make it move labels instead of drawing.


    Rod Stephens, Visual Basic Programming 24-Hour Trainer (http://www.vb-helper.com/24hourvb.htm)
    • Marked as answer by Hasithahmk Thursday, September 01, 2011 1:46 AM
    • Unmarked as answer by Hasithahmk Thursday, September 01, 2011 1:46 AM
    • Marked as answer by Leo Liu - MSFTModerator Thursday, September 01, 2011 2:17 AM
    Tuesday, August 30, 2011 10:04 PM
  • Hey Animation of Hanoi is really amazing !!!!  Thanxx!
    Hasitha
    Thursday, September 01, 2011 1:47 AM