当前位置:网站首页>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刷题14天二叉树系列之《 110 平衡二叉树判断》
- LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
- shell monitors gpu usage
- Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
- 如何给网页添加icon图标?
- LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
- [C Language] Getting Started
- MYSQLg advanced ------ clustered and non-clustered indexes
- uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
- 【力扣】22.括号生成
猜你喜欢

What is ensemble learning in machine learning?

Basic understanding of MongoDB (2)

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

Where can machine learning be applied?What is machine learning useful for?

破解事务性工作瓶颈,君子签电子合同释放HR“源动力”!

【人话版】WEB3将至之“权益的游戏”
![[FPGA] day19- binary to decimal (BCD code)](/img/d8/6d223e5e81786335a143f135385b08.png)
[FPGA] day19- binary to decimal (BCD code)

【FPGA】day18-ds18b20实现温度采集

How to learn machine learning?machine learning process

【FPGA】day19-二进制转换为十进制(BCD码)
随机推荐
洛谷P6586 蒟蒻火锅的盛宴
【服务器安装Redis】Centos7离线安装redis
如何给网页添加icon图标?
JwsManager service interface implementation class - the jni implementation
1815. Get the maximum number of groups of fresh donuts state compression
leetcode刷题第13天二叉树系列之《98 BST及其验证》
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
How to add icons to web pages?
MYSQLg advanced ------ return table
Alibaba Cloud releases 3 high-performance computing solutions
机器学习可以应用在哪些场景?机器学习有什么用?
一文读懂 高性能可预期数据中心网络
[FPGA] Design Ideas - I2C Protocol
【yolov7系列三】实战从0构建训练自己的数据集
What is Machine Reinforcement Learning?What is the principle?
0基础转行软件测试,自学3个月,浅拿12k*13薪offer
监听U盘插入 拔出 消息,获得U盘盘符
"239 Sliding Window Maximum Value" on the 16th day of LeetCode brushing
【Web3 系列开发教程——创建你的第一个 NFT(9)】如何在手机钱包里查看你的 NFT
Basic understanding of MongoDB (2)