目录:
https://blog.csdn.net/stoneWang_L/article/details/89228145
1)冒泡排序
https://blog.csdn.net/stoneWang_L/article/details/89220461
2)插入排序
https://blog.csdn.net/stoneWang_L/article/details/89221348
3)选择排序
https://blog.csdn.net/stoneWang_L/article/details/89220638
4)快速排序
https://blog.csdn.net/stoneWang_L/article/details/89221592
6)归并排序
https://blog.csdn.net/stoneWang_L/article/details/89210938
7)希尔排序
https://blog.csdn.net/stoneWang_L/article/details/89217137
堆排序
堆的概念:
每个结点的值都大于或等于其左右孩子结点的值的完全二叉树,称为大顶堆;
每个结点的值都小于或等于其左右孩子结点的值的完全二叉树,称为小顶堆;
思路:
利用堆进行排序,将待排的序列构成个大顶堆,最大值就是根节点;将根节点移走,(将其和堆末尾元素交换,此时末尾 元素就是最大值),接着将剩余的n-1个序列重新构成一个堆,继续进行。
Code:
package DataStr;
import java.util.Arrays;
/**
* 数组下标0的元素也参与排序
* @author stoneWang_L
*
*/
public class HeapSort3 {
public static void main(String[] args) {
int[] arr = {312,126,272,226,28,165,123,8,12};
// int[] arr = SortTestHelper.getRandomArray(20, 0, 2);
System.out.println("堆排序前:"+Arrays.toString(arr));
//堆排序
heapSort(arr, arr.length);
System.out.println("堆排序后:"+Arrays.toString(arr));
}
private static void heapSort(int[] arr, int count) {
//完成构建大顶堆
for(int i=(count-2)/2; i>=0; i--)
shiftDown(arr, count, i);
System.out.println("构建完成的大顶堆:"+Arrays.toString(arr));
//将大顶堆头结点(最大数)与最后一个元素交换位置,然后除去最后这个最大的元素,再shiftDown操作维护该(除去最后一个元素的数组)堆为大顶堆。循环直到升序排序完成
for(int j=count-1; j>0; j--) {
swap(arr, 0, j);
shiftDown(arr, j, 0);
}
}
/**
*
* @param arr 数组
* @param count 元素个数
* @param currentRoot 当前根节点的下标
*/
private static void shiftDown(int[] arr, int count ,int currentRoot) {
while(2*currentRoot+1 < count) {
int max = 2*currentRoot+1; //初始赋值left孩子
if(max+1 < count && arr[max+1] > arr[max])
max += 1;
if(arr[currentRoot] >= arr[max])
break;
swap(arr, currentRoot, max);
currentRoot = max;
}
}
//交换
public static void swap(int[] arr, int n, int m) {
int temp = arr[n];
arr[n] = arr[m];
arr[m] = temp;
}
}
结果: