Java实现冒泡排序

it2024-01-24  67

(1)Java实现冒泡排序

代码实现:

package com.wllarl.suanfa.sort; import java.util.Arrays; //冒泡排序 public class BubbleSort { public static void main(String[] args) { int[] array = {1,2,4,77,2,12}; BubbleSort bubbleSort = new BubbleSort(); System.out.println("+++++==============================================++++++++++++"); bubbleSort.bubbleOne(array); // System.out.println("+++++==============================================++++++++++++"); // bubbleSort.bubbleTwo(array); // System.out.println("+++++==============================================++++++++++++"); // bubbleSort.bubbleThree(array); } //冒泡排序第一个版本··· public void bubbleOne(int[] array){ int temp = 0; for (int i=0; i < array.length-1; i++){ System.out.println("第"+i+"趟结果:"); for (int j=0; j<array.length -1 ; j++){ if (array[j] > array[j+1]) { temp = array[j+1]; array[j+1] = array [j]; array[j] = temp; } System.out.println(Arrays.toString(array)); } } } //冒泡排序第二个版本 public void bubbleTwo(int[] array){ int temp = 0; for (int i=0; i < array.length-1; i++){ System.out.println("第"+i+"趟结果:"); for (int j=0; j<array.length -1-i ; j++){//改动,将前面已经排好的顺序给排除掉 if (array[j] > array[j+1]) { temp = array[j+1]; array[j+1] = array [j]; array[j] = temp; } System.out.println(Arrays.toString(array)); } } } //冒泡排序第三个版本 public void bubbleThree(int[] array){ int temp = 0; boolean flag = false;//标识符 for (int i=0; i < array.length-1; i++){ System.out.println("第"+i+"趟结果:"); for (int j=0; j<array.length -1-i ; j++){ if (array[j] > array[j+1]) { temp = array[j+1]; array[j+1] = array [j]; array[j] = temp; flag = true;//表示有交换 } System.out.println(Arrays.toString(array)); } if (!flag){ break;//表示已经有序 }else { flag = false; } } } }

实验截图: 第一版 第0趟结果: [19, 199, 4, 77, 2, 12] [19, 4, 199, 77, 2, 12] [19, 4, 77, 199, 2, 12] [19, 4, 77, 2, 199, 12] [19, 4, 77, 2, 12, 199] 第1趟结果: [4, 19, 77, 2, 12, 199] [4, 19, 77, 2, 12, 199] [4, 19, 2, 77, 12, 199] [4, 19, 2, 12, 77, 199] [4, 19, 2, 12, 77, 199] 第2趟结果: [4, 19, 2, 12, 77, 199] [4, 2, 19, 12, 77, 199] [4, 2, 12, 19, 77, 199] [4, 2, 12, 19, 77, 199] [4, 2, 12, 19, 77, 199] 第3趟结果: [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] 第4趟结果: [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] 第二版: 第0趟结果: [19, 199, 4, 77, 2, 12] [19, 4, 199, 77, 2, 12] [19, 4, 77, 199, 2, 12] [19, 4, 77, 2, 199, 12] [19, 4, 77, 2, 12, 199] 第1趟结果: [4, 19, 77, 2, 12, 199] [4, 19, 77, 2, 12, 199] [4, 19, 2, 77, 12, 199] [4, 19, 2, 12, 77, 199] 第2趟结果: [4, 19, 2, 12, 77, 199] [4, 2, 19, 12, 77, 199] [4, 2, 12, 19, 77, 199] 第3趟结果: [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] 第4趟结果: [2, 4, 12, 19, 77, 199] 第三版: 第0趟结果: [19, 199, 4, 77, 2, 12] [19, 4, 199, 77, 2, 12] [19, 4, 77, 199, 2, 12] [19, 4, 77, 2, 199, 12] [19, 4, 77, 2, 12, 199] 第1趟结果: [4, 19, 77, 2, 12, 199] [4, 19, 77, 2, 12, 199] [4, 19, 2, 77, 12, 199] [4, 19, 2, 12, 77, 199] 第2趟结果: [4, 19, 2, 12, 77, 199] [4, 2, 19, 12, 77, 199] [4, 2, 12, 19, 77, 199] 第3趟结果: [2, 4, 12, 19, 77, 199] [2, 4, 12, 19, 77, 199] 第4趟结果: [2, 4, 12, 19, 77, 199]

最新回复(0)