皇天不负有心人!在学校,我花了三年终于学会了冒泡排序!
Posted 四原色
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了皇天不负有心人!在学校,我花了三年终于学会了冒泡排序!相关的知识,希望对你有一定的参考价值。
三年如一日,编码朝朝暮暮!
终于,像所有大神一样,我学会了冒泡排序!在此之前,没有人知道我经历了多少个苦苦挣扎的日日夜夜,也没有人能够理解这颗懵懂的心!
普天之下,谁能助我?莫非是你,点赞评论加关注!
下面,带你一起去领略一下冒泡排序的精髓所在!
目录
1. 初始
冒泡排序:
总所周知,冒泡排序就是依次比较相邻的数据,将小数据放在前,大数据放在后(按从小到大排时)。
1.1 创建Sort排序接口
public interface Sort {
public int[] setArray(int num);
public int[] bubbleSort();
public void showArr(String tip,String interval);
}
1.2 实现冒泡排序
使用给出的数组,实现冒泡排序。
1.2.1 编写BubbleJunior 初级类
public class BubbleJunior implements Sort {}
1.2.2 在BubbleJunior 中添加成员变量,表示待排序/已排序数组
int arr[];
1.2.3 在BubbleJunior 中实现Sort接口中实现数组打印方法
@Override
public void showArr(String tip,String interval){
System.out.println(tip);
for (int i=0;i<this.arr.length;i++){
System.out.print(this.arr[i]+(i == arr.length-1?"":interval));
}
System.out.println();
}
1.2.4 在BubbleJunior 中实现Sort接口中实现数组生成方法
@Override
public int[] setArray(){
this.arr = new int[]{5, 99, -15, 6, 0, 18, 74, 3, 45, 2};
this.showArr("===>生成的数组为:"," ");
return arr;
}
1.2.5在BubbleJunior 中实现Sort接口中实现排序方法
@Override
public int[] bubbleSort(){
System.out.println("============================");
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
int item = this.arr[j];
this.arr[j+1] = this.arr[j];
this.arr[j] = item;
}
}
this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
1.2.6 完整BubbleJunior 类
public class BubbleJunior implements Sort{
int arr[];
public int[] setArray(int num){
this.arr = new int[]{5, 99, -15, 6, 0, 18, 74, 3, 45, 2};
this.showArr("===>生成的数组为:"," ");
return arr;
}
public int[] bubbleSort(){
System.out.println("============================");
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
int item = this.arr[j];
this.arr[j] = this.arr[j+1];
this.arr[j+1] = item;
}
}
this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
public void showArr(String tip,String interval){
System.out.println(tip);
for (int i=0;i<this.arr.length;i++){
System.out.print(this.arr[i]+(i == arr.length-1?"":interval));
}
System.out.println();
}
}
1.2.7 编写测试类
public class BubbleTest {
public static void main(String[] args) {
BubbleJunior bubbleJuniorSort =new BubbleJunior();
bubbleJuniorSort.setArray(0);
bubbleJuniorSort.bubbleSort( );
}
}
1.2.8 测试结果
1.3 加强冒泡排序
主要对优化排序过程,通过采用基本运算和异或运算两种方法去除中间变量分的方法加强交换效率。
1.3.1 编写BubbleMiddle中级类,并实现Sort接口。
public class BubbleMiddle implements Sort {}
1.3.2 优化数组排序过程,简化交换机制
- 采用基本运算去除中间变量
- 采用异或运算去除中间变量
采用基本运算去除中间变量:
this.arr[j] = this.arr[j] + this.arr[j + 1];
this.arr[j + 1] = this.arr[j] - this.arr[j + 1];
this.arr[j] = this.arr[j] - this.arr[j + 1];
采用异或运算去除中间变量:
this.arr[j] = this.arr[j] ^ this.arr[j + 1];
this.arr[j + 1] = this.arr[j] ^ this.arr[j + 1];
this.arr[j] = this.arr[j] ^ this.arr[j + 1];
1.3.3 完整的BubbleMiddle类
public class BubbleMiddle implements Sort {
int arr[];
@Override
public int[] setArray(int num) {
this.arr = new int[]{5, 99, -15, 6, 0, 18, 74, 3, 45, 2};
this.showArr("===>生成的数组为:"," ");
return arr;
}
@Override
public int[] bubbleSort() {
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
this.arr[j] = this.arr[j] + this.arr[j + 1];
this.arr[j + 1] = this.arr[j] - this.arr[j + 1];
this.arr[j] = this.arr[j] - this.arr[j + 1];
}
}
this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
public int[] bubbleSort_XOR() {
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
this.arr[j] = this.arr[j] ^ this.arr[j + 1];
this.arr[j + 1] = this.arr[j] ^ this.arr[j + 1];
this.arr[j] = this.arr[j] ^ this.arr[j + 1];
}
}
this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
@Override
public void showArr(String tip, String interval) {
System.out.println(tip);
for (int i=0;i<this.arr.length;i++){
System.out.print(this.arr[i]+(i == arr.length-1?"":interval));
}
System.out.println();
}
}
1.3.4 测试类
public class BubbleTest {
public static void main(String[] args) {
BubbleMiddle bubbleMiddle=new BubbleMiddle();
bubbleMiddle.setArray(0);
bubbleMiddle.bubbleSort();
}
}
1.3.5 测试结果
1.4 两种加强方式的排序与原始交换方式
对1.3中提出的两种优化排序过程进行比较。
- 为了增加数据量,扩大可视范围,此时加强数组生成方法,通过动态随机生成数组方式扩充数组
1.3.1 编写BubbleSenior中级类,并实现Sort接口。
public class BubbleSenior implements Sort {}
1.3.2 增强数组生成方法
@Override
public int[] setArray(int num){
this.arr = new int[num];
for (int i=0;i<this.arr.length;i++) {
this.arr[i] =(new Random()).nextInt();
}
this.showArr("===>随机生成的数组为:"," ");
return arr;
}
1.3.3 对采用原始变换中间量方式添加时间计算方法
public void invoke(int num){
long lStartTime = System.currentTimeMillis();
this.setArray(num);
this.bubbleSort();
long lEndTime = System.currentTimeMillis();
long output = lEndTime - lStartTime;
System.out.println("[1]采用原始变换中间量方式排序耗时:" + output+"ms");
}
1.3.4 对使用基本运算去除中间变量的排序方式添加时间计算方法
public void invoke_add(int num){
long lStartTime = System.currentTimeMillis();
this.setArray(num);
this.bubbleSort();
long lEndTime = System.currentTimeMillis();
long output = lEndTime - lStartTime;
System.out.println("[1]采用基本运算去除中间变量方式排序耗时:" + output+"ms");
}
1.3.5 对使用异或运算去除中间变量的排序方式添加时间计算方法
public void invokeXOR(int num){
long lStartTime = System.currentTimeMillis();
this.setArray(num);
this.bubbleSort_XOR();
long lEndTime = System.currentTimeMillis();
long output = lEndTime - lStartTime;
System.out.println("[2]采用异或运算去除中间变量方式排序耗时:" + output+"ms");
}
1.3.6 完整的BubbleSenior类
public class BubbleSenior implements Sort {
int arr[];
@Override
public int[] setArray(int num){
this.arr = new int[num];
for (int i=0;i<this.arr.length;i++) {
this.arr[i] =(new Random()).nextInt();
}
this.showArr("===>随机生成的数组为:"," ");
return arr;
}
@Override
public int[] bubbleSort() {
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
int item = this.arr[j];
this.arr[j] = this.arr[j+1];
this.arr[j+1] = item;
}
}
// this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
public int[] bubbleSort_add() {
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
this.arr[j] = this.arr[j] + this.arr[j + 1];
this.arr[j + 1] = this.arr[j] - this.arr[j + 1];
this.arr[j] = this.arr[j] - this.arr[j + 1];
}
}
// this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
public int[] bubbleSort_XOR() {
for(int i = 0;i < this.arr.length - 1;i++){
for(int j = 0;j < this.arr.length - i -1;j++){
if(this.arr[j] > this.arr[j+1] ){
this.arr[j] = this.arr[j] ^ this.arr[j + 1];
this.arr[j + 1] = this.arr[j] ^ this.arr[j + 1];
this.arr[j] = this.arr[j] ^ this.arr[j + 1];
}
}
// this.showArr("第"+(i+1)+"趟冒泡:"," ");
}
System.out.println("============================");
this.showArr("===>冒泡排序后的数组为:","<");
return this.arr;
}
@Override
public void showArr(String tip, String interval) {
System.out.println(tip);
for (int i=0;i<this.arr.length;i++){
if (i%10 == 0 && this.arr.length>10)
System.out.println();
System.out.print(this.arr[i]+(i == arr.length-1?"":interval));
}
System.out.println();
}
public void invoke(int num){
long lStartTime = System.currentTimeMillis();
this.setArray(num);
this.bubbleSort();
long lEndTime = System.currentTimeMillis();
long output = lEndTime - lStartTime;
System.out.println("[1]采用原始变换中间量方式排序耗时:" + output+"ms");
}
public void invoke_add(int num){
long lStartTime = System.currentTimeMillis();
this.setArray(num);
this.bubbleSort_add();
long lEndTime = System.currentTimeMillis();
long output = lEndTime - lStartTime;
System.out.println("[2]采用基本运算去除中间变量方式排序耗时:" + output+"ms");
}
public void invokeXOR(int num){
long lStartTime = System.currentTimeMillis();
this.setArray(num);
this.bubbleSort_XOR();
long lEndTime = System.currentTimeMillis();
long output = lEndTime - lStartTime;
System.out.println("[3]采用异或运算去除中间变量方式排序耗时:" + output+"ms");
}
}
1.3.7 测试类
public class BubbleTest {
public static void main(String[] args) {
BubbleSenior bubbleSenior = new BubbleSenior();
Scanner scanner=new Scanner(System.in);
System.out.print("==>输入生成的数组长度:");
int num=0;
while((num=scanner.nextInt())<0){
System.out.print("输入的数组长度必须大于零,请重新输入:");
}
bubbleSenior.invoke(num);
bubbleSenior.invokeXOR(num);
}
}
1.3.8 测试结果
通过在控制台输入100000,生成100000个随机整数,对比采用两种不同方式和采用原始交换方式。
(1)采用原始变换中间量方式排序耗时21198ms.
(2)采用基本运算去除中间变量方式排序耗时22314ms
(3)采用异或运算去除中间变量方式排序耗时23236ms
以上是关于皇天不负有心人!在学校,我花了三年终于学会了冒泡排序!的主要内容,如果未能解决你的问题,请参考以下文章