当前位置:网站首页>堆排序 和冒泡排序
堆排序 和冒泡排序
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
边栏推荐
- How can users overcome emotional issues in programmatic trading?
- Element's BFC attribute
- Build Zabbix Kubernetes cluster monitoring platform
- "3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
- 【FPGA】SDRAM
- 一文读懂 高性能可预期数据中心网络
- Common layout effect realization scheme
- LeetCode刷题第17天之《3 无重复字符的最长子串》
- MYSQLg高级------聚簇索引和非聚簇索引
- 【组成原理 九 CPU】
猜你喜欢

快速使用UE4制作”大场景游戏“

获取Qt的安装信息:包括安装目录及各种宏地址

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

"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series

Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods

leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》

LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"

Is Redis old?Performance comparison between Redis and Dragonfly

Detailed explanation of VIT source code

Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
随机推荐
Get the length of the linked list
The thirteenth day of learning programming
机器学习是什么?详解机器学习概念
(转)JVM中那些区域会发生OOM?
每日一题-滑动窗口
Introduction to c # a week of high-level programming c # - LINQ Day Four
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
我的 archinstall 使用手册
洛谷P4061 大吉大利,晚上吃鸡
MySQL数据库存储引擎以及数据库的创建、修改与删除
How can users overcome emotional issues in programmatic trading?
The solution to the height collapse problem
【组成原理 九 CPU】
LeetCode刷题第10天字符串系列之《125回文串验证》
AVH 动手实践 (二) | 在 Arm 虚拟硬件上部署 PP-OCR 模型
洛谷P4032 火锅盛宴
北湖区燕泉街道开展“戴头盔·保安全”送头盔活动
这些云自动化测试工具值得拥有
js uses the string as the js execution code