提交 f2e8a489 编写于 作者: M mrkam

7027689: /applets/SortDemo demo needs to be improved

Reviewed-by: rupashka
上级 0f02479b
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
/** /**
* A bi-directional bubble sort demonstration algorithm * A bi-directional bubble sort demonstration algorithm
...@@ -39,6 +37,8 @@ ...@@ -39,6 +37,8 @@
* @author James Gosling * @author James Gosling
*/ */
class BidirBubbleSortAlgorithm extends SortAlgorithm { class BidirBubbleSortAlgorithm extends SortAlgorithm {
@Override
void sort(int a[]) throws Exception { void sort(int a[]) throws Exception {
int j; int j;
int limit = a.length; int limit = a.length;
...@@ -61,9 +61,9 @@ class BidirBubbleSortAlgorithm extends SortAlgorithm { ...@@ -61,9 +61,9 @@ class BidirBubbleSortAlgorithm extends SortAlgorithm {
} }
if (!swapped) { if (!swapped) {
return; return;
} } else {
else
swapped = false; swapped = false;
}
for (j = limit; --j >= st;) { for (j = limit; --j >= st;) {
if (stopRequested) { if (stopRequested) {
return; return;
......
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
/** /**
* A bubble sort demonstration algorithm * A bubble sort demonstration algorithm
...@@ -39,23 +37,26 @@ ...@@ -39,23 +37,26 @@
* @author James Gosling * @author James Gosling
*/ */
class BubbleSortAlgorithm extends SortAlgorithm { class BubbleSortAlgorithm extends SortAlgorithm {
@Override
void sort(int a[]) throws Exception { void sort(int a[]) throws Exception {
for (int i = a.length; --i>=0; ) { for (int i = a.length; --i >= 0;) {
boolean swapped = false; boolean swapped = false;
for (int j = 0; j<i; j++) { for (int j = 0; j < i; j++) {
if (stopRequested) { if (stopRequested) {
return; return;
} }
if (a[j] > a[j+1]) { if (a[j] > a[j + 1]) {
int T = a[j]; int T = a[j];
a[j] = a[j+1]; a[j] = a[j + 1];
a[j+1] = T; a[j + 1] = T;
swapped = true; swapped = true;
} }
pause(i,j); pause(i, j);
} }
if (!swapped) if (!swapped) {
return; return;
}
} }
} }
} }
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
/** /**
* A quick sort demonstration algorithm * A quick sort demonstration algorithm
...@@ -39,8 +37,7 @@ ...@@ -39,8 +37,7 @@
* @author James Gosling * @author James Gosling
* @author Kevin A. Smith * @author Kevin A. Smith
*/ */
public class QSortAlgorithm extends SortAlgorithm public class QSortAlgorithm extends SortAlgorithm {
{
/** /**
* A version of pause() that makes it easier to ensure that we pause * A version of pause() that makes it easier to ensure that we pause
...@@ -51,84 +48,83 @@ public class QSortAlgorithm extends SortAlgorithm ...@@ -51,84 +48,83 @@ public class QSortAlgorithm extends SortAlgorithm
return true; return true;
} }
/** This is a generic version of C.A.R Hoare's Quick Sort /** This is a generic version of C.A.R Hoare's Quick Sort
* algorithm. This will handle arrays that are already * algorithm. This will handle arrays that are already
* sorted, and arrays with duplicate keys.<BR> * sorted, and arrays with duplicate keys.<BR>
* *
* If you think of a one dimensional array as going from * If you think of a one dimensional array as going from
* the lowest index on the left to the highest index on the right * the lowest index on the left to the highest index on the right
* then the parameters to this function are lowest index or * then the parameters to this function are lowest index or
* left and highest index or right. The first time you call * left and highest index or right. The first time you call
* this function it will be with the parameters 0, a.length - 1. * this function it will be with the parameters 0, a.length - 1.
* *
* @param a an integer array * @param a an integer array
* @param lo0 left boundary of array partition * @param lo0 left boundary of array partition
* @param hi0 right boundary of array partition * @param hi0 right boundary of array partition
*/ */
void QuickSort(int a[], int lo0, int hi0) throws Exception void QuickSort(int a[], int lo0, int hi0) throws Exception {
{ int lo = lo0;
int lo = lo0; int hi = hi0;
int hi = hi0; int mid;
int mid;
if ( hi0 > lo0)
{
/* Arbitrarily establishing partition element as the midpoint of if (hi0 > lo0) {
* the array.
*/
mid = a[ ( lo0 + hi0 ) / 2 ];
// loop through the array until indices cross /* Arbitrarily establishing partition element as the midpoint of
while( lo <= hi ) * the array.
{
/* find the first element that is greater than or equal to
* the partition element starting from the left Index.
*/ */
while( ( lo < hi0 ) && pauseTrue(lo0, hi0) && ( a[lo] < mid )) mid = a[(lo0 + hi0) / 2];
++lo;
/* find an element that is smaller than or equal to // loop through the array until indices cross
* the partition element starting from the right Index. while (lo <= hi) {
*/ /* find the first element that is greater than or equal to
while( ( hi > lo0 ) && pauseTrue(lo0, hi0) && ( a[hi] > mid )) * the partition element starting from the left Index.
--hi; */
while ((lo < hi0) && pauseTrue(lo0, hi0) && (a[lo] < mid)) {
++lo;
}
/* find an element that is smaller than or equal to
* the partition element starting from the right Index.
*/
while ((hi > lo0) && pauseTrue(lo0, hi0) && (a[hi] > mid)) {
--hi;
}
// if the indexes have not crossed, swap // if the indexes have not crossed, swap
if( lo <= hi ) if (lo <= hi) {
{ swap(a, lo, hi);
swap(a, lo, hi); ++lo;
++lo; --hi;
--hi; }
} }
}
/* If the right index has not reached the left side of array /* If the right index has not reached the left side of array
* must now sort the left partition. * must now sort the left partition.
*/ */
if( lo0 < hi ) if (lo0 < hi) {
QuickSort( a, lo0, hi ); QuickSort(a, lo0, hi);
}
/* If the left index has not reached the right side of array /* If the left index has not reached the right side of array
* must now sort the right partition. * must now sort the right partition.
*/ */
if( lo < hi0 ) if (lo < hi0) {
QuickSort( a, lo, hi0 ); QuickSort(a, lo, hi0);
}
} }
} }
private void swap(int a[], int i, int j) private void swap(int a[], int i, int j) {
{ int T;
int T; T = a[i];
T = a[i]; a[i] = a[j];
a[i] = a[j]; a[j] = T;
a[j] = T;
} }
public void sort(int a[]) throws Exception @Override
{ public void sort(int a[]) throws Exception {
QuickSort(a, 0, a.length - 1); QuickSort(a, 0, a.length - 1);
} }
} }
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
/** /**
* A generic sort demonstration algorithm * A generic sort demonstration algorithm
...@@ -38,13 +36,12 @@ ...@@ -38,13 +36,12 @@
* *
* @author James Gosling * @author James Gosling
*/ */
class SortAlgorithm { class SortAlgorithm {
/** /**
* The sort item. * The sort item.
*/ */
private SortItem parent; private SortItem parent;
/** /**
* When true stop sorting. * When true stop sorting.
*/ */
......
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,57 +29,50 @@ ...@@ -29,57 +29,50 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
import java.awt.*; import java.awt.Color;
import java.awt.event.*; import java.awt.Dimension;
import java.io.InputStream; import java.awt.Graphics;
import java.util.Hashtable; import java.awt.event.MouseEvent;
import java.net.*; import java.awt.event.MouseListener;
/** /**
* A simple applet class to demonstrate a sort algorithm. * A simple applet class to demonstrate a sort algorithm.
* You can specify a sorting algorithm using the "alg" * You can specify a sorting algorithm using the "alg"
* attribyte. When you click on the applet, a thread is * attribute. When you click on the applet, a thread is
* forked which animates the sorting algorithm. * forked which animates the sorting algorithm.
* *
* @author James Gosling * @author James Gosling
*/ */
public class SortItem @SuppressWarnings("serial")
extends java.applet.Applet public class SortItem extends java.applet.Applet implements Runnable,
implements Runnable, MouseListener { MouseListener {
/** /**
* The thread that is sorting (or null). * The thread that is sorting (or null).
*/ */
private Thread kicker; private Thread kicker;
/** /**
* The array that is being sorted. * The array that is being sorted.
*/ */
int arr[]; int arr[];
/** /**
* The high water mark. * The high water mark.
*/ */
int h1 = -1; int h1 = -1;
/** /**
* The low water mark. * The low water mark.
*/ */
int h2 = -1; int h2 = -1;
/** /**
* The name of the algorithm. * The name of the algorithm.
*/ */
String algName; String algName;
/** /**
* The sorting algorithm (or null). * The sorting algorithm (or null).
*/ */
SortAlgorithm algorithm; SortAlgorithm algorithm;
Dimension initialSize = null; Dimension initialSize = null;
/** /**
...@@ -91,10 +84,10 @@ public class SortItem ...@@ -91,10 +84,10 @@ public class SortItem
double f = initialSize.width / (double) a.length; double f = initialSize.width / (double) a.length;
for (int i = a.length; --i >= 0;) { for (int i = a.length; --i >= 0;) {
a[i] = (int)(i * f); a[i] = (int) (i * f);
} }
for (int i = a.length; --i >= 0;) { for (int i = a.length; --i >= 0;) {
int j = (int)(i * Math.random()); int j = (int) (i * Math.random());
int t = a[i]; int t = a[i];
a[i] = a[j]; a[i] = a[j];
a[j] = t; a[j] = t;
...@@ -128,12 +121,16 @@ public class SortItem ...@@ -128,12 +121,16 @@ public class SortItem
if (kicker != null) { if (kicker != null) {
repaint(); repaint();
} }
try {Thread.sleep(20);} catch (InterruptedException e){} try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
} }
/** /**
* Initialize the applet. * Initialize the applet.
*/ */
@Override
public void init() { public void init() {
String at = getParameter("alg"); String at = getParameter("alg");
if (at == null) { if (at == null) {
...@@ -147,6 +144,7 @@ public class SortItem ...@@ -147,6 +144,7 @@ public class SortItem
addMouseListener(this); addMouseListener(this);
} }
@Override
public void start() { public void start() {
h1 = h2 = -1; h1 = h2 = -1;
scramble(); scramble();
...@@ -157,6 +155,7 @@ public class SortItem ...@@ -157,6 +155,7 @@ public class SortItem
/** /**
* Deallocate resources of applet. * Deallocate resources of applet.
*/ */
@Override
public void destroy() { public void destroy() {
removeMouseListener(this); removeMouseListener(this);
} }
...@@ -165,10 +164,11 @@ public class SortItem ...@@ -165,10 +164,11 @@ public class SortItem
* Paint the array of numbers as a list * Paint the array of numbers as a list
* of horizontal lines of varying lengths. * of horizontal lines of varying lengths.
*/ */
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
int a[] = arr; int a[] = arr;
int y = 0; int y = 0;
int deltaY = 0, deltaX = 0, evenY = 0, evenX = 0; int deltaY = 0, deltaX = 0, evenY = 0;
Dimension currentSize = getSize(); Dimension currentSize = getSize();
int currentHeight = currentSize.height; int currentHeight = currentSize.height;
...@@ -184,7 +184,6 @@ public class SortItem ...@@ -184,7 +184,6 @@ public class SortItem
// even size. // even size.
if (!currentSize.equals(initialSize)) { if (!currentSize.equals(initialSize)) {
evenY = (currentHeight - initialSize.height) % 2; evenY = (currentHeight - initialSize.height) % 2;
evenX = (currentWidth - initialSize.width) % 2;
deltaY = (currentHeight - initialSize.height) / 2; deltaY = (currentHeight - initialSize.height) / 2;
deltaX = (currentWidth - initialSize.width) / 2; deltaX = (currentWidth - initialSize.width) / 2;
...@@ -194,7 +193,6 @@ public class SortItem ...@@ -194,7 +193,6 @@ public class SortItem
} }
if (deltaX < 0) { if (deltaX < 0) {
deltaX = 0; deltaX = 0;
evenX = 0;
} }
} }
...@@ -227,6 +225,7 @@ public class SortItem ...@@ -227,6 +225,7 @@ public class SortItem
/** /**
* Update without erasing the background. * Update without erasing the background.
*/ */
@Override
public void update(Graphics g) { public void update(Graphics g) {
paint(g); paint(g);
} }
...@@ -238,15 +237,16 @@ public class SortItem ...@@ -238,15 +237,16 @@ public class SortItem
* @see java.lang.Thread#run * @see java.lang.Thread#run
* @see SortItem#mouseUp * @see SortItem#mouseUp
*/ */
@Override
public void run() { public void run() {
try { try {
if (algorithm == null) { if (algorithm == null) {
algorithm = (SortAlgorithm)Class.forName(algName).newInstance(); algorithm = (SortAlgorithm) Class.forName(algName).newInstance();
algorithm.setParent(this); algorithm.setParent(this);
} }
algorithm.init(); algorithm.init();
algorithm.sort(arr); algorithm.sort(arr);
} catch(Exception e) { } catch (Exception e) {
} }
} }
...@@ -254,8 +254,9 @@ public class SortItem ...@@ -254,8 +254,9 @@ public class SortItem
* Stop the applet. Kill any sorting algorithm that * Stop the applet. Kill any sorting algorithm that
* is still sorting. * is still sorting.
*/ */
@Override
public synchronized void stop() { public synchronized void stop() {
if (algorithm != null){ if (algorithm != null) {
try { try {
algorithm.stop(); algorithm.stop();
} catch (IllegalThreadStateException e) { } catch (IllegalThreadStateException e) {
...@@ -279,35 +280,42 @@ public class SortItem ...@@ -279,35 +280,42 @@ public class SortItem
} }
} }
@Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
showStatus(getParameter("alg")); showStatus(getParameter("alg"));
} }
@Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
} }
/** /**
* The user clicked in the applet. Start the clock! * The user clicked in the applet. Start the clock!
*/ */
@Override
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
startSort(); startSort();
e.consume(); e.consume();
} }
@Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
} }
@Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
} }
@Override
public String getAppletInfo() { public String getAppletInfo() {
return "Title: SortDemo \nAuthor: James Gosling 1.17f, 10 Apr 1995 \nA simple applet class to demonstrate a sort algorithm. \nYou can specify a sorting algorithm using the 'alg' attribute. \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm."; return "Title: SortDemo \nAuthor: James Gosling 1.17f, 10 Apr 1995 \nA simple applet class to demonstrate a sort algorithm. \nYou can specify a sorting algorithm using the 'alg' attribute. \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm.";
} }
@Override
public String[][] getParameterInfo() { public String[][] getParameterInfo() {
String[][] info = { String[][] info = {
{"alg", "string", "The name of the algorithm to run. You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.' BubbleSort is the default. Example: Use 'QSort' to run the QSortAlgorithm class."} { "alg", "string",
"The name of the algorithm to run. You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.' BubbleSort is the default. Example: Use 'QSort' to run the QSortAlgorithm class." }
}; };
return info; return info;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<h1>The Sorting Algorithm Demo (1.1)</h1> <h1>The Sorting Algorithm Demo (1.1)</h1>
<hr> <hr>
<table cols=3 width="340"> <table width="340">
<tr> <tr>
<th><strong>Bubble Sort</strong></th> <th><strong>Bubble Sort</strong></th>
<th><strong>Bidirectional Bubble Sort</strong></th> <th><strong>Bidirectional Bubble Sort</strong></th>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册