快速排序(quick sort)
快速排序是一种高效的排序方式,它是一种分区交换排序方式,它类似冒泡,danshi相比于冒泡逐个比较替换元素的方式,它更高效,直接交换元素。并且根据设定的基准值分区自治,
程序思路
首先取第一个元素做基准值,用来区分高位区域,和地位区域。
从右边往左边找,找到一个小于基准值的元素,交换到低位元素
从左边往右边找,找到一个大于基准值的,交换到高位区域元素
如此以往,根据基准值分区,在得到一个新的基准值,不停分区,不停比较交换,最终排序完成。使用递归完成此操作。
Sorting_quicksort_anim.gif
public static void main(String[] args) {
int[] a = new int[20];
for (int i = 0; i < 20; i++) {
int random = (int) (Math.random() * 10);
a[i] = random;
}
printArrays(a);
quickSort(a, 0, a.length - 1);
}
private static void quickSort(int[] a, int leftIdx, int rightIdx) {
if (rightIdx >=leftIdx) {
//基准值
int temp = a[leftIdx];
int tempLeft = leftIdx;
int tempRight = rightIdx;
while (tempLeft < tempRight) {
while (tempLeft < tempRight && a[tempRight] >= temp) {
//找到一个小于基准值的,退出,否则继续循环
tempRight--;
}
a[tempLeft] = a[tempRight];
while (tempLeft < tempRight && a[tempLeft] <= temp) {
//找到一个大于基准值的,退出,否则继续循环
tempLeft++;
}
a[tempRight] = a[tempLeft];
printArrays(a);
}
a[tempLeft] = temp;
//对基准值左边的元素进行递归排序
quickSort(a, leftIdx, tempLeft - 1);
//对基准值右边的元素进行递归排序。
quickSort(a, tempRight + 1, rightIdx);
}
}
private static void printArrays(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "、");
}
System.out.println();
}
执行结果》》
9、0、4、2、2、5、0、1、1、9、5、1、6、7、9、8、3、2、8、9、
8、0、4、2、2、5、0、1、1、9、5、1、6、7、9、8、3、2、8、9、
2、0、4、2、2、5、0、1、1、9、5、1、6、7、9、8、3、9、9、9、
2、0、4、2、2、5、0、1、1、3、5、1、6、7、9、8、9、9、9、9、
2、0、4、2、2、5、0、1、1、3、5、1、6、7、9、8、9、9、9、9、
1、0、4、2、2、5、0、1、1、3、5、4、6、7、8、8、9、9、9、9、
1、0、1、2、2、5、0、1、5、3、5、4、6、7、8、8、9、9、9、9、
1、0、1、2、2、1、0、1、5、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、2、2、1、2、2、5、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、2、2、1、2、2、5、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、1、2、1、2、2、5、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、1、2、1、2、2、5、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、1、1、1、2、2、5、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、1、1、2、2、2、4、3、5、4、6、7、8、8、9、9、9、9、
0、0、1、1、1、2、2、2、3、3、5、5、6、7、8、8、9、9、9、9、
0、0、1、1、1、2、2、2、3、4、5、5、6、7、8、8、9、9、9、9、
0、0、1、1、1、2、2、2、3、4、5、5、6、7、8、8、9、9、9、9、
0、0、1、1、1、2、2、2、3、4、5、5、6、7、8、8、9、9、9、9、
堆排序(Heepsort)
堆是一个近似完全二叉树的结构,并同时满足堆积的性质。
树是一种数据结构,由根,枝,叶构成,
tree.jpeg
最顶部的节点称之为根节点,
最外层的节点称之为叶节点
有子节点的称之为枝节点
二叉树是一种特殊的树结构
每个节点只能有两个子节点,
Binary tree.jpge.jpg
完全二叉树是一种特殊的二叉树结构,他不仅是每个节点都最多有两个子节点,而且子节点的顺序是从最左侧开始添加,往最右侧添加,中间没有任何空缺,
tree.png
在实际的堆排序操作中,我们有两种选择,大顶堆,小顶堆,大顶堆思想就是将大的数字推上根节点,最后将根结点和末尾节点交换。小顶堆反之。
Heapsort-example.gif
Sorting_heapsort_anim.gif
在实现代码之前,我们需要理解完全二叉树的数据结构特点:
有父子结构的节点为i,那么它的左子节点为2i+1,右子节点为2i+2,它的父节点为( i - 1)/ 2(节点从0开始计算)
有了这样的数据结构的支持,我们就可以通过代码来将一个链表用堆来操作了。
public static void main(String[] args) {
int[] a = new int[20];
for (int i = 0; i < 20; i++) {
int random = (int) (Math.random() * 10);
a[i] = random;
}
printArrays(a);
heapSort(a, a.length - 1);
printArrays(a);
}
private static void heapSort(int[] a, int len) {
if (len < 0) {
return;
}
for (int i = len; i >= 0; i--) {
//比较当前节点和父节点大小
int parent = (i - 1) / 2;
int p = a[parent];
if (a[i] > p) {
a[parent] = a[i];
a[i] = p;
}
}
//将跟节点和末尾节点交换
int temp = a[len];
a[len] = a[0];
a[0] = temp;
//将末尾节点踢出树结构,下次不在比较
len--;
heapSort(a, len);
}
private static void printArrays(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "、");
}
System.out.println();
}
简书传送门>>>>常见排序算法
个人博客传送门>>>> 个人博客