Page 1 of 1

## 0 Replies - 1110 Views - Last Post: 28 March 2007 - 02:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=25679&amp;s=b3e94562d069cf684bc107f2b38ff8d1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lwnexgen

Reputation: 1
• Posts: 12
• Joined: 27-March 07

Posted 28 March 2007 - 02:02 AM

Hey Guys-

I'm kind of at my wits end with this problem. I have to create a method to sort a vector into increasing order. I can get it to compile and run with a few initial vectors, but I run into a StackOverflow sometimes, meaning my recursion isn't closing right at some point in my method.

For instance, the input <1,3,2,5,4> works correctly. <1,5,3,4,2> does not, however.

Here is my code right now (its a very ugly version of the QuickSort algorithm):

```public void sort(Vector<Integer> v)
{
Vector<Integer> less = new Vector<Integer>();
Vector<Integer> equal = new Vector<Integer>();
Vector<Integer> greater = new Vector<Integer>();

v = sort(v,0,v.size()-1,v.size()/2,less,equal,greater);
} // end of sort method

private Vector<Integer> sort(Vector<Integer> v, int start, int end, int pivot, Vector<Integer> less,Vector<Integer> equal,Vector<Integer> greater)
{
System.out.println(v);
if (start == v.size())
{
return v;
}

if (pivot - 1 <=0)
{
return v;
}

int pivotVal = v.get(pivot-1);

if (v.get(start)< pivotVal)
{
sort(v,start+1,end, pivot,less,equal,greater);
}
else if (v.get(start)>pivotVal)
{
sort(v,start+1,end, pivot,less,equal,greater);
}
else if (v.get(start)==pivotVal)
{
sort(v,start+1,end, pivot,less,equal,greater);
}

v.clear();
return v;
}

```