我有一个对数组进行排序的applet。在一个JPanel
上,它使用用户选择的排序对一个数组进行排序,而在另一个JPanel
上排序,它同时使用所有四个,每个都在其线程中。
同时代码4。
private class startAH implements ActionListener {
public void actionPerformed(ActionEvent e){
allArrays.clear();
bubble.clear();
selection.clear();
quick.clear();
merge.clear();
startBut.setEnabled(false);
threadStart = true;
loopJump = (int) spinner.getValue();
loopFinish = loopJump*10;
currentPercentBubble =0;
currentPercentSelection =0;
currentPercentMerge =0;
currentPercentQuick =0;
bubbleBool=false;
selectionBool=false;
quickBool=false;
mergeBool=false;
saveBut.setEnabled(false);
iBubble=0;
iSelection=0;
iQuick=0;
iMerge=0;
new Thread(new Runnable(){
public void run(){
for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
{
Long elapsedTime=(long) 0;
Long currentTime;
for(int innerLoop = 0; innerLoop < 10; innerLoop++){
long startTime = System.currentTimeMillis();
try {
int[] intArray = new int[outLoop];
sort.fillArray(intArray);
sort.bubbleSort(intArray);
} catch (InterruptedException e) {
}
long endTime = System.currentTimeMillis();
currentTime = endTime-startTime;
elapsedTime +=currentTime;
currentPercentBubble++;
repaint();
}
long finalTime = (long) (elapsedTime/10);
String[] current = new String[2];
current[0]=""+outLoop;
current[1]=""+finalTime;
bubble.add(iBubble, current);
iBubble++;
}
allArrays.add(3, bubble);
bubbleBool = true;
saveCheck();
repaint();
}
}).start();
new Thread(new Runnable(){
public void run(){
for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
{
Long elapsedTime=(long) 0;
Long currentTime;
for(int innerLoop = 0; innerLoop < 10; innerLoop++){
long startTime = System.currentTimeMillis();
try {
int[] intArray = new int[outLoop];
sort.fillArray(intArray);
sort.selectionSort(intArray);
} catch (InterruptedException e) {
}
long endTime = System.currentTimeMillis();
currentTime = endTime-startTime;
elapsedTime +=currentTime;
currentPercentSelection++;
repaint();
}
long finalTime = (long) (elapsedTime/10);
String[] current = new String[2];
current[0]=""+outLoop;
current[1]=""+finalTime;
selection.add(iSelection, current);
iSelection++;
}
allArrays.add(2, selection);
selectionBool = true;
saveCheck();
repaint();
}
}).start();
new Thread(new Runnable(){
public void run(){
for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
{
Long elapsedTime=(long) 0;
Long currentTime;
for(int innerLoop = 0; innerLoop < 10; innerLoop++){
long startTime = System.currentTimeMillis();
try {
int[] intArray = new int[outLoop];
sort.fillArray(intArray);
sort.quickSort(intArray);
} catch (InterruptedException e) {
}
long endTime = System.currentTimeMillis();
currentTime = endTime-startTime;
elapsedTime +=currentTime;
currentPercentMerge++;
repaint();
}
long finalTime = (long) (elapsedTime/10);
String[] current = new String[2];
current[0]=""+outLoop;
current[1]=""+finalTime;
quick.add(iQuick, current);
iQuick++;
}
allArrays.add(0, quick);
quickBool = true;
saveCheck();
repaint();
}
}).start();
new Thread(new Runnable(){
public void run(){
for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
{
Long elapsedTime=(long) 0;
Long currentTime;
for(int innerLoop = 0; innerLoop < 10; innerLoop++){
long startTime = System.currentTimeMillis();
try {
int[] intArray = new int[outLoop];
sort.fillArray(intArray);
sort.mergeSort(intArray);
} catch (InterruptedException e) {
}
long endTime = System.currentTimeMillis();
currentTime = endTime-startTime;
elapsedTime +=currentTime;
currentPercentQuick++;
repaint();
}
long finalTime = (long) (elapsedTime/10);
String[] current = new String[2];
current[0]=""+outLoop;
current[1]=""+finalTime;
merge.add(iMerge, current);
iMerge++;
}
allArrays.add(1, merge);
mergeBool = true;
saveCheck();
repaint();
}
}).start();
}
}
单身代码:
private class startAH implements ActionListener {
public void actionPerformed(ActionEvent e){
new Thread(new Runnable(){
public void run(){
singleSortStart = true;
currentPercent = 0;
startBut.setEnabled(false);
try {
selectedSort = (String)listBox.getSelectedItem();
selectSort((String)listBox.getSelectedItem(), numbersForSort);
} catch (InterruptedException e) {
e.printStackTrace();
}
saveBut.setEnabled(true);
startBut.setEnabled(true);
}
}).start();
}
}
他们使用来自另一个类的排序,我希望能够允许用户在运行时停止它们而不使它们使用不同的排序方法。有什么想法吗?
各种代码:
public class Sorts {
static Random arrayFill = new Random();
static final int GREATESTNUMBER = 100;
public void fillArray(int[] array){
for(int i = 0; i < array.length; i++)
array[i] = arrayFill.nextInt(GREATESTNUMBER);
}
/**
*
* @param array
* @throws InterruptedException
*/
public void bubbleSort(int[] array) throws InterruptedException {
int n = array.length;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
int k = i + 1;
if (array[i] > array[k]) {
swap(i, k, array);
}
}
}
}
/**
*
* @param i
* @param j
* @param array
*/
private void swap(int i, int j, int[] array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
/**
*
* @param array
* @throws InterruptedException
*/
public void selectionSort(int[] array) throws InterruptedException {
for (int i = 0; i < array.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < array.length; j++)
if (array[j] < array[index])
index = j;
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
//delayThread();
}
}
/**
*
* @param a
* @throws InterruptedException
*/
public void mergeSort(int[ ] a) throws InterruptedException
{
int[] tmp = new int[a.length];
mergeSort(a, tmp, 0, a.length - 1);
}
/**
*
* @param a
* @param tmp
* @param left
* @param right
* @throws InterruptedException
*/
private void mergeSort(int[ ] a, int[ ] tmp, int left, int right) throws InterruptedException
{
if( left < right )
{
int center = (left + right) / 2;
mergeSort(a, tmp, left, center);
mergeSort(a, tmp, center + 1, right);
merge(a, tmp, left, center + 1, right);
}
}
/**
*
* @param a
* @param tmp
* @param left
* @param right
* @param rightEnd
* @throws InterruptedException
*/
private void merge(int[ ] a, int[ ] tmp, int left, int right, int rightEnd )throws InterruptedException
{
int leftEnd = right - 1;
int k = left;
int num = rightEnd - left + 1;
while(left <= leftEnd && right <= rightEnd)
if(a[left]<=a[right])
tmp[k++] = a[left++];
else
tmp[k++] = a[right++];
while(left <= leftEnd) // Copy rest of first half
tmp[k++] = a[left++];
while(right <= rightEnd) // Copy rest of right half
tmp[k++] = a[right++];
// Copy tmp back
for(int i = 0; i < num; i++, rightEnd--){
a[rightEnd] = tmp[rightEnd];
//delayThread();
}
}
/**
*
* @param array
* @throws InterruptedException
*/
public void quickSort(int array[]) throws InterruptedException
// pre: array is full, all elements are non-null integers
// post: the array is sorted in ascending order
{
quickSort(array, 0, array.length - 1);// quicksort all the elements in the array
}
/**
*
* @param array
* @param start
* @param end
* @throws InterruptedException
*/
public void quickSort(int[] arr, int low, int high) throws InterruptedException {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
//pick the pivot
int middle = low + (high - low) / 2;
int pivot = arr[middle];
//make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
//recursively sort two sub parts
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
}
我有另一个使用不同排序和延迟的JPanel,所以我可以显示正在排序的数组,我可以用这个停止线程
private void delayThread() throws InterruptedException
{
repaint();
Thread.sleep(slider.getValue());
if(stopThread){
startBut.setEnabled(true);
stopBut.setEnabled(false);
stopThread = false;
listBox.setEnabled(true);
throw new RuntimeException();
}
}
并且每种类型都有延迟
private void bubbleSortD(int[] array) throws InterruptedException
{
int n = array.length;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
int k = i + 1;
if (array[i] > array[k]) {
swapD(i, k, array);
}
delayThread();
}
}
}
但我无法找到一种方法来完成这个以及单独和4使用不同的排序,就像这样,我不想写排序3次,如果有人有办法得到1,这将是惊人的。 顺便说一下,他们都是不同的班级。