当前位置:网站首页>Bubble sort and heap sort
Bubble sort and heap sort
2022-08-11 04:27:00 【mmmenxj】
1. Heap sort is nlogN level sort:
public static void heapSort(int[] arr){//1. First perform heapify on arr and adjust it to the maximum heap//Start the shiftDown operation from the last non-leaf nodefor (int i = (arr.length -1 -1)>>1; i >= 0; i--) {shiftDown(arr, i, arr.length);}//At this time, arr is adjusted to the maximum heapfor (int i = arr.length -1; i >0 ; i--) {//arr[0] is the top element of the heap, which is the maximum value of the current heapswap(arr,0,i);shiftDown(arr,0,i);}}// element sink operationprivate static void shiftDown(int[] arr, int i, int length) {while(2* i +1 arr[j]){j = j+1;}//j is the maximum value of the left and right subtreesif(arr[i] >arr[j]){// end of sinkingbreak;}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. Bubble sort o(n^2)
public static void bubbleSort(int[] arr){for (int i = 0; i arr[j +1]){swap(arr,j,j+1);isSwaped = true;}}if(!isSwaped){break;//It has been completely ordered}} Heap and priority queue:
1. Maximum heap, minimum heap implementation
2. Heap sort
3.TopK problem
17.14 -- 343--373
边栏推荐
- Leetcode 450. 删除二叉搜索树中的节点
- MySQL database storage engine and database creation, modification and deletion
- Pinduoduo store business license related issues
- What is Machine Reinforcement Learning?What is the principle?
- Which one to choose for mobile map development?
- js 将字符串作为js执行代码使用
- LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
- 【FPGA】abbreviation
- shell监视gpu使用情况
- 【深度学习】基于卷积神经网络的天气识别训练
猜你喜欢

【组成原理 九 CPU】

Harvesting of radio frequency energy

es-head插件插入查询以及条件查询(五)

Power Cabinet Data Monitoring RTU
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

【深度学习】基于卷积神经网络的天气识别训练

LeetCode刷题第10天字符串系列之《125回文串验证》

Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods

这些云自动化测试工具值得拥有

What is machine learning?Explain machine learning concepts in detail
随机推荐
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
What are port 80 and port 443?What's the difference?
【FPGA】day21- moving average filter
「转」“搜索”的原理,架构,实现,实践,面试不用再怕了
JVM 垃圾回收的概述与机制
MySQL database storage engine and database creation, modification and deletion
redis按照正则批量删除key
洛谷P5139 z小f的函数
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
Interchangeable Measurement Techniques - Geometric Errors
这些云自动化测试工具值得拥有
js 将字符串作为js执行代码使用
洛谷P4324 扭动的回文串
"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
增加PRODUCT_BOOT_JARS及类 提供jar包给应用
洛谷P2245 星际导航
【人话版】WEB3将至之“权益的游戏”
Which one to choose for mobile map development?
[Server installation Redis] Centos7 offline installation of redis
es-head插件插入查询以及条件查询(五)