当前位置:网站首页>堆排序 和冒泡排序
堆排序 和冒泡排序
2022-08-11 04:00:00 【mmmenxj】
1.堆排序是nlogN级别的排序:
public static void heapSort(int[] arr){
//1.先将arr进行heapify调整为最大堆
//从最后一个非叶子结点开始进行shiftDown操作
for (int i = (arr.length -1 -1)>>1; i >= 0; i--) {
shiftDown(arr, i, arr.length);
}
//此时arr调整为最大堆
for (int i = arr.length -1; i >0 ; i--) {
//arr[0]就是堆顶元素,就是当前堆的最大值
swap(arr,0,i);
shiftDown(arr,0,i);
}
}
//元素下沉操作
private static void shiftDown(int[] arr, int i, int length) {
while(2* i +1 <length){
int j = (i<<1) +1;
if(j + 1<length && arr[j +1] >arr[j]){
j = j+1;
}
//j就是左右子树的最大值
if(arr[i] >arr[j]){
//下沉结束
break;
}else{
swap(arr,i,j);
i = j;
}
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {7,5,4,3,1,2,10,9,8};
heapSort(arr);
System.out.println(Arrays.toString(arr));
}2.冒泡排序o(n^2)
public static void bubbleSort(int[] arr){
for (int i = 0; i <arr.length -1; i++) {
boolean isSwaped = false;
for (int j = 0; j <arr.length -1 -i; j++) {
if(arr[j] >arr[j +1]){
swap(arr,j,j+1);
isSwaped = true;
}
}
if(!isSwaped){
break;//已经彻底有序了
}
}堆和优先级队列:
1.最大堆、最小堆实现
2.堆排序
3.TopK问题
17.14 -- 343--373
边栏推荐
- "104 Maximum Depth of Binary Trees" in LeetCode's Day 12 Binary Tree Series
- 洛谷P2370 yyy2015c01 的 U 盘
- MySQL database storage engine and database creation, modification and deletion
- 【FPGA】day21- moving average filter
- 《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇
- 80端口和443端口是什么?有什么区别?
- 使用jackson解析json数据详讲
- 【力扣】22.括号生成
- UNI-APP_iphone bottom safe area
- typedef defines the structure array type
猜你喜欢

2022-08-10 The sixth group Hiding spring study notes

什么是机器强化学习?原理是什么?

这些云自动化测试工具值得拥有
![[FPGA] day19- binary to decimal (BCD code)](/img/d8/6d223e5e81786335a143f135385b08.png)
[FPGA] day19- binary to decimal (BCD code)

"125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions

【FPGA】day19-二进制转换为十进制(BCD码)

CTO said that the number of rows in a MySQL table should not exceed 2000w, why?

LeetCode刷题第17天之《3 无重复字符的最长子串》

机器学习可以应用在哪些场景?机器学习有什么用?

机器学习中什么是集成学习?
随机推荐
每日一题-滑动窗口
洛谷P4324 扭动的回文串
【FPGA】名词缩写
Qnet Weak Network Test Tool Operation Guide
Get the length of the linked list
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
获取Qt的安装信息:包括安装目录及各种宏地址
使用百度EasyDL实现森林火灾预警识别
Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
MYSQLg advanced ------ return table
洛谷P5139 z小f的函数
Leetcode 450. 删除二叉搜索树中的节点
Provincial level of Echart maps, as well as all prefecture-level download and use
WPF DataGrid 使用数据模板(2)
LeetCode刷题第17天之《3 无重复字符的最长子串》
洛谷P4061 大吉大利,晚上吃鸡
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
How to learn machine learning?machine learning process
Description of ESB product development steps under cloud platform
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started