Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

837 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