冒泡排序及其优化

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;

	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值