RSS Forums RSS
Please support our C# advertiser: Programming Forums

Array Index Problem in Quick Sort

Join Date: Mar 2007
Posts: 2
Reputation: jslicer is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jslicer jslicer is offline Offline
Newbie Poster

Re: Array Index Problem in Quick Sort

  #4  
Mar 27th, 2007
I prefer to remove the tail recursion and have a while loop take care of the case where i < hi as such:

[html]private static void quicks(int [] arr, int lo, int hi)
{
int i;

do
{
i = lo;

int j = hi;
int pivot = arr[(lo + hi) / 2];

do
{
while (arr[i] < pivot)
{
i++;
}
while (arr[j] > pivot)
{
j--;
}
if (i <= j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
} while (i <= j);
if (lo < j)
{
quicks(arr, lo, j);
}
lo = i;
} while (i < hi);
}[/html]
now, unless you have a massively huge array (say, 2^20 or thereabouts), you shouldn't have any recursive stack overflow issues.
Last edited by jslicer : Mar 27th, 2007 at 1:30 pm. Reason: reformatting
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:49 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC