Results 1 to 7 of 7

Thread: Threading and recursive functions problem.[solved]

  1. #1

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64

    Resolved Threading and recursive functions problem.[solved]

    Hi, i'll briefly try to explain what i'm trying to do:

    For programming class i got to prepare a project that demonstrates QuickSort algorythm. This means i have to create an animation that simulates the sorting process: the elements in the array to be sorted, the swapping of elements in each partition (i'm implementing "in place quicksort" algorythm).
    The animation runs ok, i'm using labels to depict each number in the array to be sorted, but my problem is the one requirement that sais that my program should implement pausing and step-by-step runnning of the animation and various speed settings. this is the C# code i'm using, sorry for not having vb.net version, still on c# forums noone had any ideea:

    Code:
    public void _swapBars(int L ,int  H)
    		{
    			int i;
    			
    			Bar low = (Bar)(bare[L]);//Bar object is a control that inherits Label
    			Bar high = (Bar)(bare[H]);
    			int posHigh = high.Left ;
    			for (i=0;i<120;i++)
    			{
    				low.Top--;
    				high.Top--;
    			};
    			
    			
    			if (L!=H)
    			{
    
    				do
    				{
    					
    					low.Left++ ;
    					high.Left--;
    				}while (low.Left<posHigh);
    			}
    
    			for (i=0;i<120;i++)
    			{
    				low.Top++;
    				high.Top++;
    			}
    			
    			bare[L]=high;
    			bare[H]=low;
    			         
    
    		}

    Where should i look into so i can control the speed of the animation and the breakpoint-like runnning mode ?

    something similar to what i'm looking for is presented on this page:
    http://ciips.ee.uwa.edu.au/~morris/...10/qsort1a.html
    check for the [run quicksort animation] button located on the bottom of the page.
    Last edited by mindloop; Mar 31st, 2005 at 04:16 PM.
    ehmm...

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: expert help needed: How to implement debugging-like breakpoint into program

    I would suggest perhaps storing your variables outside of the method itself, then split your code up whereby you have n graphically discernable sections.
    Then perhaps create an overloaded version of the method that allows someone to jump directly in at a particular piece of the code.

    So that way, if someone hits pause, you record where you were last and jump out of the method. Then when they hit play, your variables haven't been disposed of because they're outside of the method, and you can jump straight in at one particular point of the code.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64

    Re: expert help needed: How to implement debugging-like breakpoint into program

    seems pretty close to what i'm trying to do.

    thx
    ehmm...

  4. #4

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64

    Re: Threading and recursive functions problem.

    Hmm i just went splitting my code when i realised i cant do that for a recursive function like quicksort.

    i would need some further assistance as i was digging into msdn and decided to use threads.

    so instead calling recursive function directley i started a new thread with a method that calls quicksort.
    basically i didnt change anything just that my function now runs in a separate thread.
    the problem is that the function runs only once and refuses to call itself recursiveley.
    if anyone can explain to me what is wrong please post here
    ill post my code:
    Code:
    public static Hashtable bare = new Hashtable();
    private bool stopFlag=false;
    private Thread SThread;
    ...
    private void sortThread()
    {
    	qSort(0,bare.Count-1);
    }
    private void qSort(int prim, int ultim)
    {
    	int i,j; i=prim; j=ultim;
    	Bar baraPivot;
    	baraPivot=((Bar)(bare[(prim+ultim)/2])) ;
     	int pivot = baraPivot.Val;
    	do
    	{
    		while(((Bar)bare[i]).Val <pivot) i++;
    		while(((Bar)(bare[j])).Val >pivot) j--;
    		if (i<=j)
    		{	_swapBars(i,j);//function presented in previous post
    			i++;j--;		
    		};
    	}while(i<=j && !stopFlag);//i use stopFlag for 
    //aborting function by user intervention at runtime.
    	if(prim<j && !stopFlag)  qSort(prim,j);//quicksort for left partition
    	if(i<ultim && !stopFlag) qSort(i,ultim);//quicksort for right partition
    }
    so when i call quicksort directley:
    Code:
    void BStartClick(object sender, System.EventArgs e)
    {
    	qSort(0,bare.Count-1);
    }
    functions runs smoothley and sorts the elements correctley but when i run it by starting new thread :
    Code:
    void BStartClick(object sender, System.EventArgs e)
    {
    	SThread = new Thread(new ThreadStart(sortThread));
    	SThread.Start();
    }
    the function stops after first call (_swapbars is called only once) and ignores recursive calls.
    is there something i'm missing about threads ? are recursive functions a nono for multithreading ?
    ehmm...

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Threading and recursive functions problem.

    Using multi-threading is overcomplicating things.

    You can convert a recursive function into a loop by using a stack.
    Each item on the stack represents a call to the recursive function.
    The items should be a type consisting of all the parameters needed for the function.

    You push you first 'call' onto the stack, having set its parameters. Then you enter a while loop, repeating until the stack is empty.

    In the loop you pop from the stack, deal with the parameters as you would in the recursive function, then anywhere you'd do a recursive call you simple push the parameters onto the stack.

    In this way you should be able to display output at times round the loop showing things like the contents of the array (parameter or global, depending on how the recursive function worked), how deep in the recursion you are (stack size) and the parameters of the current recursive call (the popped off data).
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64

    Re: Threading and recursive functions problem.

    Oh well, that might work aswell but i just fisnihed rewriting my code (3'th time already) , this time for thread usage. so i'd really appreciate an oppinion bout the previous post's issue. where is the problem with my code?

    SLH: thx for the stack ideea (i'll prolly end up using it), yet threading looks like the closest thing for my needs, i have to be able to stop and resume execution at any point within qSort() function. Anyway i've been documenting on Threads so i'm curious why my function is not working.
    ehmm...

  7. #7

    Thread Starter
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64

    Re: Threading and recursive functions problem. [solved]

    i figured out what the problem was... somehow stopflag went true at some point, really no clue at all how, it probably has something to do with thread and variable types, ima check it out.
    so recursive function was ok with multithreading.
    thx all.
    ehmm...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width