Can anyone explain line 39, 40, 41, 42, 43?

import java.awt.*;
import javax.swing.*;

public class Quicksort extends JApplet
String output="";
public void init ()
{ // initialize
int a[] = {37,2,6,4,89,8,10,12,68,45};

output+= "The initial array is: \t";
for (int i=0; i<a.length; i++)
output+= a[i] +" ";

// call recursive function
quickSort (a, 0, (a.length -1) );

output+= "\nThe sorted array is: \t";
for (int i=0; i<a.length; i++)
output+= a[i] +" ";

JTextArea outputArea = new JTextArea (80, 15);
JScrollPane scroller = new JScrollPane (outputArea);
Container c = getContentPane();
outputArea.setText (output);
} // end init method

public void quickSort(int b[], int st, int end)
{ int finalPos;

// base case
if (end-st+1 <= 1) return; // don't partition anymore

// main recursive subroutine
else // continue to partition and quickSort the subarrays
finalPos = partition (b, st, end);  //??
if ( ((finalPos-1)-st +1) >1 )      //?
quickSort(b, st, finalPos-1);       //?
if ( end-(finalPos+1)+1 >1 )        //?
quickSort (b, finalPos+1, end);     //?

} // end quickSort method definition
Who is Participating?
imladrisConnect With a Mentor Commented:
I don't know how familiar you are with recursion and quick sort, so if you wanted more detail, please ask.

The basic idea of a quick sort is to take a list of items and partition it into 2 pieces. There must be some swapping to ensure that each portion contains the "correct" elements. Having achieved that (which would happen here with the partition method, for which the code is not listed), you then proceed to check if the two "half" lists contain more than one item or not. If they do, they you call quicksort for the "half" list. (Half is in quotes, because there is actually no requirement that the two lists be the same size).

So again, there are two potentially big and confusing topics here: recursion and quicksort. Recursion occurs when a routine calls itself. Quicksort is a particular sorting algorithm (usually defined recursively) that is very fast (in terms of number of swaps).

No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to imladris

Please leave any comments here within the
next seven days.


Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.