Java~常见排序之选择排序、堆排序、冒泡排序

**基本原理也是选择排序,只是不在使用遍历的方式查找无序区间的最大的数,而是通过堆来选择无序区间的最大的数。

PS:排升序要建大堆;排降序要建小堆。**

在这里插入图片描述

在这里插入图片描述

实现:

//堆排序

public void heapSort(int[] array) {

createHeap(array);

//直接将数组长度减一,下次向下调整对最后一个元素不做处理,heapSize等于0的时候就只有一个元素为排序

for (int heapSize = array.length - 1; heapSize > 0; heapSize–) {

int tmp = array[0];

array[0] = array[heapSize];

array[heapSize] = tmp;

shiftDown(array,heapSize,0);

}

}

private void createHeap(int[] array) {

//第一次减一是得到最后一个元素,在减一是为了找它的双亲结点

for (int i = (array.length -1 - 1) / 2; i >= 0 ; i–) {

shiftDown(array,array.length,i);

}

}

private void shiftDown(int[] array, int size, int index) {

int parent = index;

int child = 2 * parent + 1;

while (child < size) {

if(child + 1 < size && array[child + 1] > array[ child]) {

child ++;

}

if(array[child] > array[parent]) {

int tmp = array[child];

array[child] = array[parent];

array[parent] = tmp;

}else {

break;

}

parent = child;

child = 2 * parent + 1;

}

}

性能分析:

  • 时间复杂度

O(NlogN)

  • 空间复杂度

O(1)

冒泡排序- 原理(稳定)

在无序区间,通过相邻数的比较,将最大的数冒泡到无序区间的最后,持续这个过程,直到数组整体有序。

在这里插入图片描述

实现:

//冒泡排序,从前到后

public void bubbleSort(int[] array) {

for (int bound = 0; bound < array.length; bound++) {

//最后一个元素不用排序所以减一,已排好的bound个也不用对其进行比较处理

for (int i = 0; i < array.length -1 -bound; i++) {

if(array[i] > array[i + 1]) {

int tmp = array[i];

array[i] = array[i + 1];

array[i + 1] = tmp;

}

}

}

}

//冒泡排序从后到前

public void bubbleSort1(int[] array) {

for (int bound = 0; bound < array.length; bound++) {

for(int i = array.length - 1; i > bound; i --) {

if(array[i - 1] > array[i]) {

int tmp = array[i];

array[i] = array[i - 1];

array[i - 1] = tmp;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值