import javax.swing.JApplet;
import javax.swing.JTextArea;
import java.awt.Container;
public class BubbleSort1 extends JApplet {
public void init() {
this.setSize(350, 250);
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add(outputArea);
outputArea.setEditable(false);
int array[] = { 45, 5, 23, 9, 10, 15, 19, 17, 25, 14, 17 };
String output = "Data item in original order\n";
for (int counter = 0; counter < array.length; counter++)
output += " " + array[counter];
bubbleSort(array);
output += "\n\nData items in ascending order\n";
for (int counter = 0; counter < array.length; counter++)
output += " " + array[counter];
outputArea.setText(output);
}
public void bubbleSort(int array[]) {
for (int i = 0; i < array.length - 1; i++)
for (int j = 0; j < array.length - 1 - i; j++)
if (array[j] > array[j + 1])
swap(array, j); // 一定要注意传递方式
}
public void swap(int[] array, int j) {
int k = array[j];
array[j] = array[j + 1];
array[j + 1] = k;
}
}
注意数组传递方式!!!
/*本程序根据BubbleSort1改编,传递j和j+1的值进行交换,注意将数组应该在类体里声明,而不是方法里声明*/
import javax.swing.JApplet;
import javax.swing.JTextArea;
import java.awt.Container;
public class BubbleSort2 extends JApplet {
int array[] = { 45, 5, 23, 9, 10, 15, 19, 17, 25, 14, 17 };
public void init() {
this.setSize(350, 250);
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add(outputArea);
outputArea.setEditable(false);
String output = "Data item in original order\n";
for (int counter = 0; counter < array.length; counter++)
output += " " + array[counter];
bubbleSort(array);
output += "\n\nData items in ascending order\n";
for (int counter = 0; counter < array.length; counter++)
output += " " + array[counter];
outputArea.setText(output);
}
public void bubbleSort(int array[]) {
for (int i = 0; i < array.length - 1; i++)
for (int j = 0; j < array.length - 1 - i; j++)
if (array[j] > array[j + 1])
swap(j, j + 1);
}
public void swap(int j, int i) {
int k = array[j];
array[j] = array[i];
array[i] = k;
}
}
开始优化,并写测试
//优化后的冒泡排序;
import java.awt.Container;
import javax.swing.JApplet;
import javax.swing.JTextArea;
public class BubbleSortPro extends JApplet {
int step = 0;
boolean flag;
// int array[] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int array[] = { 1, 2, 3, 4, 5, 6, 8, 7, 9, 10 };
public void init() {
this.setSize(300, 300);
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add(outputArea);
String output = "Data items in original order\n";
for (int counter = 0; counter < array.length; counter++) {
output += " " + array[counter];
}
bubbleSort(array);
output += "\n\nData items in asscending order\n";
for (int counter = 0; counter < array.length; counter++) {
output += " " + array[counter];
}
outputArea.setText(output);
System.out.println("执行比较的次数:" + step);
}
public void bubbleSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
flag = true;
for (int j = 0; j < array.length - i - 1; j++) {
step++; // 计算执行的比较次数
if (array[j] > array[j + 1]) {
swap(j, j + 1);
flag = false;
}
}
if (flag == true)
break;
}
}
public void swap(int j1, int j2) {
int k = array[j1];
this.array[j1] = this.array[j2];
array[j2] = k;
}
}