Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Infinite recursion - base case ignored and stack overflowed

807588Jul 29 2009 — edited Jul 29 2009
Hi, I've been having this logic error for 2 days, and I cannot fix it. Right now, I can barely see anything because I was looking at computer screen for 10 hours. The code I'm writing is a program that allows the user to play a list of MP3 files in a folder. Among commands we have to make, we have to sort the list according to authors, dates, and titles as desired by the user. This specific problem I'm having is from the sorting method. We have to use quick sort, and through the help of textbook and online, I was able to write the code as follows. The problem is, I'm having an infinite recursion. The base case I wrote and if else statement are completely ignored and the method keeps calling itself. Perhaps, return statement is not how you are supposed to end a recurssive method? I have no idea. Any help will be appreciated. Thank you in advance.
public void sort (Comparator<MP3File> comp) {
		System.out.println("first: " + first  + "last: " + last);
		quickSort(first, last, comp);
		System.out.println("done");
		return;
	}
	
	public void quickSort (int left, int right, Comparator<MP3File> comp) {
		int leftBound= left;
		int rightBound= right;
		System.out.println("before sorting: left - " + left + " right - " + right);
		
		if (left>=right) {
			return;
		}
		 if ( ( left < 0 ) || ( left >= listOfMP3.length ) || 
		           ( right < 0 ) || ( right >= listOfMP3.length ) ) {
		         throw new IndexOutOfBoundsException();
		      }
		
		 if (right-left>=1) {
			 System.out.println("difference between right and left: " + (right-left));
			 MP3File pivot = listOfMP3[left];
			 
			 while (rightBound>leftBound)
			 {
				 while (comp.compare(listOfMP3[leftBound], pivot)<= 0 && leftBound<=right &&rightBound>leftBound) {
					 leftBound++;
					 System.out.println("leftBound " + leftBound);
				 }
				 while (comp.compare(listOfMP3[rightBound], pivot)>0 && rightBound>=left &&rightBound>=leftBound) {
					 rightBound--;
					 System.out.println("rightBound " + rightBound);
				 }
				 if (rightBound>leftBound) {
					 swap (leftBound,rightBound);
				 }

				swap(left, rightBound);
				System.out.println("swapped");
				System.out.println("calling left sorting");
				quickSort(left, rightBound-1, comp);
				System.out.println("calling right sorting");
				quickSort(rightBound+1, right, comp);

			 }
			
		 }
		 else {
			 System.out.println("wtf");
			 return;
		 }
		 System.out.println("error");
		 return;
	}
	
	public void swap(int index1, int index2) {
		MP3File temp = listOfMP3[index1];
		listOfMP3[index1] = listOfMP3[index2];
		listOfMP3[index2] = temp;
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2009
Added on Jul 29 2009
3 comments
281 views