473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

recurrsive function goes into infinte loop

nabh4u
62
hi, i have a functionn sort_and_count where i will sort a vector and count the number of inversions. i will first divide the list into two n call the function recurrsively. problem is its going on,doesnt stop. please can you tell me why is this problem here? this implements merge sort also.
here's the sample code:

Expand|Select|Wrap|Line Numbers
  1. int sort_and_count(int Left,int Right)
  2. {
  3.      int Mid;
  4.      int count=0,countL=0,countR=0;
  5.      if(Left==Right)
  6.      {
  7.           return count=0;
  8.      }
  9.      else
  10.      {
  11.           Mid=(Left+Right+1)/2;
  12.           countL = sort_and_count(Left,Mid);
  13.           countR = sort_and_count(Mid+1,Right);
  14.           count = merge_count(Left,Mid,Right);
  15.           return count=count+countL+countR;
  16.      }    
  17. }
thank you in advance..
Mar 11 '07 #1
3 1739
iLL
63
hi, i have a functionn sort_and_count where i will sort a vector and count the number of inversions. i will first divide the list into two n call the function recurrsively. problem is its going on,doesnt stop. please can you tell me why is this problem here? this implements merge sort also.
here's the sample code:

Expand|Select|Wrap|Line Numbers
  1. int sort_and_count(int Left,int Right)
  2. {
  3.      int Mid;
  4.      int count=0,countL=0,countR=0;
  5.      if(Left==Right)
  6.      {
  7.           return count=0;
  8.      }
  9.      else
  10.      {
  11.           Mid=(Left+Right+1)/2;
  12.           countL = sort_and_count(Left,Mid);
  13.           countR = sort_and_count(Mid+1,Right);
  14.           count = merge_count(Left,Mid,Right);
  15.           return count=count+countL+countR;
  16.      }    
  17. }
thank you in advance..

There is something wrong with your equation

1 = (0 + 1 + 1)/2 // Integral math

In the next line you state sort_and_count(Left,Mid); since Mid == Right, you’ll get into an infinite loop here.

Any time the difference between left and right is 1, then Mid == Right


therefore you’ll never get to the base case of left == right
Mar 11 '07 #2
iLL
63
Maybe (note that I haven’t try it) if your base case is:

if((Right == Left) || (Right – Left = 1))
Mar 11 '07 #3
nabh4u
62
There is something wrong with your equation

1 = (0 + 1 + 1)/2 // Integral math

In the next line you state sort_and_count(Left,Mid); since Mid == Right, you’ll get into an infinite loop here.

Any time the difference between left and right is 1, then Mid == Right


therefore you’ll never get to the base case of left == right
thanks for the insight...i have resolved the error...
the equations should be :
mid = (left+right)/2
Mar 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Brad Tilley | last post by:
I have a function that starts a socket server looping continuously listening for connections. It works. Clients can connect to it and send data. The problem is this: I want to get the data that...
0
by: imassadpk | last post by:
Hi all, Apprecite your help in resolving this tricky issue... I am trying implementing Recurrsive TreeView in MS. Access 2003 ADP project. So far, no luck :( The Problem: This sample...
15
by: Jens.Toerring | last post by:
Hi, I have a possibly rather stupid question about the order of evaluation in a statement like this: result = foo( x ) - bar( y ); How can I make 100% sure that foo(x) is evaluated before...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: Dawid Kuroczko | last post by:
Suppose I have vacuum_values() function, which removes all "no longer referenced" by parent column. Kind of function to be run from time to time to clean table from crud. It looks like this:...
9
by: Jimakos Bilakis | last post by:
Hi everyone! I want to create a function that it will take as parameters a table (float), an integer (the number of Table's rows) and it will re-arrange the elements in the table from the...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
1
by: mithilesh kumar | last post by:
hi , I'm mithilesh from bangalore. I'm trying to write GCD program in recursive function. my logic is while(m!=n) { if(m>n) {m=m/n; else n=n/m;}
35
by: erik gartz | last post by:
Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.