Solved

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

Posted on 2002-05-15
3
191 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

785 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