1.9.3 冒泡排序
介绍了解:
思路:
- 每一趟排序将一个元素排序在一极
- 优化思路:如果发现某一趟排序没有发生一次元素交换,那么可以判定该数组已经是有序的,即可结束冒泡排序
实现代码:
未优化前的冒泡排序
public static void bubbleSort(int[] array){ // 未优化前的冒泡排序
System.out.println("未优化前冒泡排序: ");
System.out.println("排序前数组: "+ Arrays.toString(array));
long startTime = System.currentTimeMillis(); //起始时间
// 默认升序排序
for(int i=0;i<array.length-1;i++){ // 比如10个数字就需要排序9趟
for(int j=0;j<array.length-1-i;j++){ // 每一趟需要对比9次数字大小,每一趟排序完可以确定一个极值数字,于是可以少对比一次数字大小
if(array[j]>array[j+1]){
// 若前一位数值大于后一位数值,交换数值
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
System.out.println("第"+i+"趟排序结果: "+Arrays.toString(array));
}
long endTime = System.currentTimeMillis();
System.out.println("排序花费时间: "+(endTime-startTime));
}
测试代码:
int[] array01 = {3,9,-1,10,-2};
bubbleSort(array01);
输出结果:
未优化前冒泡排序:
排序前数组: [3, 9, -1, 10, -2]
第0趟排序结果: [3, -1, 9, -2, 10]
第1趟排序结果: [-1, 3, -2, 9, 10]
第2趟排序结果: [-1, -2, 3, 9, 10]
第3趟排序结果: [-2, -1, 3, 9, 10]
优化后的冒泡排序
- 优化思路是定义一个flag变量,当在一趟排序的时候若有数据发生了交换,那么代表该数组还没完全有序,继续下一趟排序。若没有发生数据交换,那么代表该数组已然有序,即可跳出循环,结束排序。
- 优化适用场景:
- 此优化适用于数据量大且数据比较有序的情况。
- 如果数据量小的而且数据乱序比较严重的话,此优化的效果比较差
public static void bubbleSort2(int[] array){
System.out.println("优化后冒泡排序: ");
System.out.println("排序前数组: "+ Arrays.toString(array));
long startTime = System.currentTimeMillis(); //起始时间
// 默认升序排序
boolean flag;
for(int i=0;i<array.length-1;i++){ // 比如10个数字就需要排序9趟
flag = false;
for(int j=0;j<array.length-1-i;j++){ // 每一趟需要对比9次数字大小,每一趟排序完可以确定一个极值数字,于是可以少对比一次数字大小
if(array[j]>array[j+1]){
flag = true;
// 若前一位数值大于后一位数值,交换数值
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
System.out.println("第"+i+"趟排序结果: "+Arrays.toString(array));
if (!flag){
System.out.println("该数组已有序排列!");
break;
}
}
long endTime = System.currentTimeMillis();
System.out.println("排序花费时间: "+(endTime-startTime));
}
测试代码:
int[] array01 = {3,9,-1,10,-2};
bubbleSort2(array01);
输出结果:
优化后冒泡排序:
排序前数组: [3, 9, -1, 10, -2]
第0趟排序结果: [3, -1, 9, -2, 10]
第1趟排序结果: [-1, 3, -2, 9, 10]
第2趟排序结果: [-1, -2, 3, 9, 10]
第3趟排序结果: [-2, -1, 3, 9, 10]
在数据量大情况下两种排序的花费时间比较 :
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Random;
public class bubbleSorting {
public static void bubbleSort2(int[] array){
System.out.println("优化后冒泡排序: ");
// System.out.println("排序前数组: "+ Arrays.toString(array));
long startTime = System.currentTimeMillis(); //起始时间
// 默认升序排序
boolean flag;
for(int i=0;i<array.length-1;i++){ // 比如10个数字就需要排序9趟
flag = false;
for(int j=0;j<array.length-1-i;j++){ // 每一趟需要对比9次数字大小,每一趟排序完可以确定一个极值数字,于是可以少对比一次数字大小
if(array[j]>array[j+1]){
flag = true;
// 若前一位数值大于后一位数值,交换数值
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
// System.out.println("第"+i+"趟排序结果: "+Arrays.toString(array));
if (!flag){
// System.out.println("该数组已有序排列!");
break;
}
}
long endTime = System.currentTimeMillis();
System.out.println("排序花费时间: "+(endTime-startTime));
}
public static void bubbleSort(int[] array){ // 未优化前的冒泡排序
System.out.println("未优化前冒泡排序: ");
// System.out.println("排序前数组: "+ Arrays.toString(array));
long startTime = System.currentTimeMillis(); //起始时间
// 默认升序排序
for(int i=0;i<array.length-1;i++){ // 比如10个数字就需要排序9趟
for(int j=0;j<array.length-1-i;j++){ // 每一趟需要对比9次数字大小,每一趟排序完可以确定一个极值数字,于是可以少对比一次数字大小
if(array[j]>array[j+1]){
// 若前一位数值大于后一位数值,交换数值
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
// System.out.println("第"+i+"趟排序结果: "+Arrays.toString(array));
}
long endTime = System.currentTimeMillis();
System.out.println("排序花费时间: "+(endTime-startTime));
}
public static void main(String[] args) {
// 生成随机无序数组
int[] array = new int[1000];
Random random = new Random();
for(int i=0;i<1000;i++){
array[i] = random.nextInt(1500);
}
bubbleSort(array);
bubbleSort2(array);
}
}
输出结果:
未优化前冒泡排序:
排序花费时间: 4
优化后冒泡排序:
排序花费时间: 0
1.9.4 选择排序
介绍:
思路:
实现代码与输出结果:
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
public class selectSortDemo {
public static void selectSort(int[] array){ // 默认升序排序
System.out.println("排序前: "+ Arrays.toString(array));
long startTime = System.currentTimeMillis();
for (int i=0;i<array.length-1;i++){ // n个数排序只需要排序n-1趟
int minIndex = i; // 初始化最小值和最小值下标
int min;
for (int j=i+1;j<array.length;j++){ // 从第i+1个数字开始寻找极值
if (array[minIndex] > array[j]){
minIndex = j; // 更新记录的最小数的下标
}
}
if (minIndex != i){ // 只要minIndex发生了变化 就需要交换最小值,确保最小值处于最左端
min = array[minIndex];
array[minIndex] = array[i];
array[i] = min;
}
System.out.println("第"+i+"趟选择排序: "+Arrays.toString(array));
}
long endTime = System.currentTimeMillis();
System.out.println("排序花费时间: "+(endTime-startTime));
}
public static void main(String[] args) {
// 测试代码一
int[] array = {101,34,119,1,-1,90,123};
selectSort(array);
// 测试代码二: 因为数据太多,建议把打印数组代码注释
// int[] array2 = new int[1000];
// Random random = new Random();
// for(int i=0;i<1000;i++){ // 生成随机无序数组
// array2[i] = random.nextInt(1500);
// }
// selectSort(array2);
}
}
输出结果:
排序前: [101, 34, 119, 1, -1, 90, 123]
第0趟选择排序: [-1, 34, 119, 1, 101, 90, 123]
第1趟选择排序: [-1, 1, 119, 34, 101, 90, 123]
第2趟选择排序: [-1, 1, 34, 119, 101, 90, 123]
第3趟选择排序: [-1, 1, 34, 90, 101, 119, 123]
第4趟选择排序: [-1, 1, 34, 90, 101, 119, 123]
第5趟选择排序: [-1, 1, 34, 90, 101, 119, 123]
排序花费时间: 1
1.9.5 插入排序
介绍:
思路:
- 将原来数组分为两份表,一份为有序表,开始时默认只有原数组的第一个元素。另外一份为无序表,为原数组未排序的元素集合
- 每次遍历无序表一个元素,将该元素插入到有序表内,在插入过程中需要根据插入元素的大小判别插入的位置
- 看如下动态图加深理解
- 在往有序表插入新元素的时候,会先在原有序表最后一个数字后一位空一个位置temp出来(该位置实际上是插入数字insertValue的位置)
- 然后插入数字insertValue会与temp前一位数字frontValue进行大小比较
- 若insertValue < frontValue 那么temp向前移动一位,frontValue向后移动一位。之后insertValue重新会与temp新的前一位数字frontValue进行数值比较
- 直到insertValue > frontValue 或者 将有序表所有数字都比较过(这就代表insertValue比有序表最小数据还小,直接插入有序表第一个位置即可)
- 当跳出while循环的时候insertValue指针指向的是insertValue要插入位置的前一位地址,所以插入赋值的时候需要将insertIndex+1

实现代码与输出结果:
import java.lang.reflect.Array;
import java.util.Arrays;
public class insertSort {
public static void insertSortFunction(int[] array){ // 默认从小到大排序
System.out.println("排序前: "+ Arrays.toString(array));
for (int i=1;i<array.length;i++){ // 第一个元素默认加入到有序表内
int insertValue = array[i];
int insertIndex = i-1;
// 判断插入数据insertValue是否处于array[0]——array[End]之间
// 想要修改排序为降序只需要修改 insertValue < array[insertIndex]
while (insertIndex >=0 && insertValue < array[insertIndex]){
array[insertIndex+1] = array[insertIndex]; // 将frontValue后移一位
insertIndex--; // 将temp前移一位
}
// 当跳出循环的时候 说明找到插入数据的插入位置了,并insertIndex指针指向插入位置前一位
array[insertIndex+1] = insertValue; // 插入值
System.out.println("第"+i+"趟插入排序: "+Arrays.toString(array));
}
}
public static void main(String[] args) {
int[] array = {101,34,119 ,1,-1,89};
insertSortFunction(array);
}
}
输出结果:
排序前: [101, 34, 119, 1, -1, 89]
第1趟插入排序: [34, 101, 119, 1, -1, 89]
第2趟插入排序: [34, 101, 119, 1, -1, 89]
第3趟插入排序: [1, 34, 101, 119, -1, 89]
第4趟插入排序: [-1, 1, 34, 101, 119, 89]
第5趟插入排序: [-1, 1, 34, 89, 101, 119]