Solved

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

Posted on 2002-05-15
3
189 Views
Last Modified: 2010-03-31
import java.awt.*;
import javax.swing.*;

public class Quicksort extends JApplet
{
String output="";
//37,2,6,4,89,8,10,12,68,45};//
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();
c.add(scroller);
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
0
Comment
Question by:brett605
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 7011843
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).

0
 
LVL 35

Expert Comment

by:girionis
ID: 8749750
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now