冒泡排序
最好情况:Ο(n)
最坏情况:Ο(n2)
平均情况:Ο(n2)
辅助空间:Ο(C)
稳定性:稳定
冒泡排序是一种简单的算法,它重复地走访过要排序的元素,一次比较相邻两个元素,如果他们的顺序错误就把他们调换过来,直到没有元素再需要交换,排序完成。这个算法的名字由来是因为越小(或越大)的元素会经由交换慢慢“浮”到数列的顶端。
过程:
- 遍历比较相邻相邻元素
1.1. 如果前比后大,则调换他们的位置 - 缩小遍历范围,继续从头遍历
冒泡排序由两个循环构成,内循环保证每次都能拿出最大的元素排在最前面,所以外循环会组件限制内循环的范围
public static void main(String[] args) {
int[] arr = new int[]{5, 8, 6, 14, 2, 4, 10, 9, 1, 7, 3, 0, 13};
int length = arr.length;
for (int j = 0; j < length - 1; j++) { //外循环每次限制内循环遍历的范围
for (int i = 0; i < length - 1 - j; i++) {
if (arr[i] > arr[i + 1]) { //比较大小,交换位置
exchange(arr, i, i + 1);
}
}
print(arr);
}
}
static void exchange(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
static void print(int[] arr) {
for (int a : arr) { //输出
System.out.print(a + "\t");
}
System.out.println();
}
输出:
5 6 8 2 4 10 9 1 7 3 0 13 14
5 6 2 4 8 9 1 7 3 0 10 13 14
5 2 4 6 8 1 7 3 0 9 10 13 14
2 4 5 6 1 7 3 0 8 9 10 13 14
2 4 5 1 6 3 0 7 8 9 10 13 14
2 4 1 5 3 0 6 7 8 9 10 13 14
2 1 4 3 0 5 6 7 8 9 10 13 14
1 2 3 0 4 5 6 7 8 9 10 13 14
1 2 0 3 4 5 6 7 8 9 10 13 14
1 0 2 3 4 5 6 7 8 9 10 13 14
0 1 2 3 4 5 6 7 8 9 10 13 14
0 1 2 3 4 5 6 7 8 9 10 13 14
改进
上述算法是冒泡最基本的算法,但是在多数情况下会浪费最后几次外循环。
下面是改进的冒泡排序,加入一个布尔型,来判断是否经历了交换。
public static void main(String[] args) {
int[] arr = new int[]{5, 8, 6, 14, 2, 4, 10, 9, 1, 7, 3, 0, 13};
int length = arr.length;
for (int j = 0; j < length - 1; j++) { //外循环每次限制内循环遍历的范围
boolean unsort = true;
for (int i = 0; i < length - 1 - j; i++) {
if (arr[i] > arr[i + 1]) { //比较大小,交换位置
exchange(arr, i, i + 1);
unsort &= false;
}
}
print(arr);
if (unsort) { //如果内循环没有发生位置交换,则已经排序完毕,跳出即可
break;
}
}
}
输出:
5 6 8 2 4 10 9 1 7 3 0 13 14
5 6 2 4 8 9 1 7 3 0 10 13 14
5 2 4 6 8 1 7 3 0 9 10 13 14
2 4 5 6 1 7 3 0 8 9 10 13 14
2 4 5 1 6 3 0 7 8 9 10 13 14
2 4 1 5 3 0 6 7 8 9 10 13 14
2 1 4 3 0 5 6 7 8 9 10 13 14
1 2 3 0 4 5 6 7 8 9 10 13 14
1 2 0 3 4 5 6 7 8 9 10 13 14
1 0 2 3 4 5 6 7 8 9 10 13 14
0 1 2 3 4 5 6 7 8 9 10 13 14
尽管冒泡排序是最容易了解和实现的排序算法之一,但它对于少数元素之外的数列排序是很没有效率的。
鸡尾酒排序
最好情况:Ο(n)
最坏情况:Ο(n2)
平均情况:Ο(n2)
辅助空间:Ο(C)
稳定性:稳定
鸡尾酒排序是基于冒泡排序的改进算法,也叫定向冒泡排序。与冒泡不同的是,鸡尾酒排序在部分元素规律的情况下的性能有所提高,但完全乱序情况下其效率与冒泡一样差。
过程:
- 确定左右边界
- 从前向后遍历相邻元素
2.1. 如果前比后大,则交换元素位置 - 从后向前遍历相邻元素
3.1. 如果后比前小,则交换元素位置 - 缩小左右边界
冒泡排序仅从前向后遍历序列元素,鸡尾酒排序不仅从前向后遍历,也从后向前遍历。
public static void main(String[] args) {
int[] arr = new int[]{5, 8, 6, 14, 2, 4, 10, 9, 1, 7, 3, 0, 13};
int left = 0; //左边界
int right = arr.length - 1; //右边界
while (left < right) {
boolean unsort = true; //是否交换位置的标志
for (int i = 0; i < right; i++) { //从左至右,把最大放在后面
if (arr[i] > arr[i + 1]) {
exchange(arr, i, i + 1);
unsort &= false;
}
}
right--;
for (int i = right; i > left; i--) { //从右至左,把最小放在前面
if (arr[i] < arr[i - 1]) {
exchange(arr, i, i - 1);
unsort &= false;
}
}
left++;
print(arr);
if (unsort) { //如果内循环没有发生位置交换,则已经排序完毕,跳出即可
break;
}
}
}
输出:
0 5 6 8 2 4 10 9 1 7 3 13 14
0 1 5 6 2 4 8 9 3 7 10 13 14
0 1 2 5 3 4 6 8 7 9 10 13 14
0 1 2 3 4 5 6 7 8 9 10 13 14
0 1 2 3 4 5 6 7 8 9 10 13 14
0 1 2 3 4 5 6 7 8 9 10 13 14
在乱数序列的状态下,鸡尾酒排序与冒泡排序的效率都很差劲。
选择排序
最好情况:Ο(n2)
最坏情况:Ο(n2)
平均情况:Ο(n2)
辅助空间:Ο(C)
稳定性:不稳定
选择排序也是一种简单直观的排序算法。它的工作原理很容易理解:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置;然后,再从剩余未排序元素中继续寻找最小(大)元素,放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
过程:
- 将序列分为两部分:排序序列和未排序序列,确定范围
- 在未排序序列中找到最小元素的指针
- 最小元素与未排序序列第一个元素进行交换
- 调整排序序列和未排序序列的范围
public static void main(String[] args) {
int[] arr = new int[]{5, 8, 6, 14, 2, 4, 10, 9, 1, 7, 3, 0, 13};
int length = arr.length;
for (int i = 0; i < length - 1; i++) { //i代表从前向后逐渐增加排序序列的长度。如果一个序列长度是10,那么排序9次即可
int min = i; //初始化最小元素的位置,为未排序序列第一个元素
for (int j = i + 1; j < length; j++) { //从前向后在未排序序列中找最小元素
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) { //不稳定时刻
exchange(arr, min, i);
}
print(arr);
}
}
输出:
0 8 6 14 2 4 10 9 1 7 3 5 13
0 1 6 14 2 4 10 9 8 7 3 5 13
0 1 2 14 6 4 10 9 8 7 3 5 13
0 1 2 3 6 4 10 9 8 7 14 5 13
0 1 2 3 4 6 10 9 8 7 14 5 13
0 1 2 3 4 5 10 9 8 7 14 6 13
0 1 2 3 4 5 6 9 8 7 14 10 13
0 1 2 3 4 5 6 7 8 9 14 10 13
0 1 2 3 4 5 6 7 8 9 14 10 13
0 1 2 3 4 5 6 7 8 9 14 10 13
0 1 2 3 4 5 6 7 8 9 10 14 13
0 1 2 3 4 5 6 7 8 9 10 13 14
选择排序是不稳定的排序算法,不稳定发生在最小元素与A[i]交换的时刻。
来源:http://www.cnblogs.com/eniac12/p/5329396.html