当前位置:网站首页>堆排序 和冒泡排序
堆排序 和冒泡排序
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
边栏推荐
- 80端口和443端口是什么?有什么区别?
- MySQL database storage engine and database creation, modification and deletion
- Day20 FPGA 】 【 - block the I2C read and write EEPROM
- Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
- Common layout effect realization scheme
- Detailed explanation of VIT source code
- C language recv() function, recvfrom() function, recvmsg() function
- Build Zabbix Kubernetes cluster monitoring platform
- DNS separation resolution and intelligent resolution
- 【FPGA】SDRAM
猜你喜欢
什么是机器强化学习?原理是什么?
Power Cabinet Data Monitoring RTU
Get Qt installation information: including installation directory and various macro addresses
电力机柜数据监测RTU
机器学习是什么?详解机器学习概念
云平台下ESB产品开发步骤说明
UNI-APP_iphone bottom safe area
这些云自动化测试工具值得拥有
【FPGA】设计思路——I2C协议
Is Redis old?Performance comparison between Redis and Dragonfly
随机推荐
Binary tree related code questions [more complete] C language
洛谷P2150 寿司晚宴
LeetCode刷题第17天之《3 无重复字符的最长子串》
【FPGA】day21-移动平均滤波器
Qnet Weak Network Test Tool Operation Guide
A brief analysis of whether programmatic futures trading or manual order is better?
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
如何进行AI业务诊断,快速识别降本提效增长点?
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
云平台下ESB产品开发步骤说明
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
shell监视gpu使用情况
Leetcode 450. 删除二叉搜索树中的节点
LeetCode刷题第16天之《239滑动窗口最大值》
Get the length of the linked list
DNS separation resolution and intelligent resolution
C# 一周入门高级编程之《C#-LINQ》Day Four
uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)