当前位置:网站首页>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
边栏推荐
猜你喜欢

无线电射频能量的收集
![Binary tree related code questions [more complete] C language](/img/85/a109eed69cd54be3c8290e8dd67b7c.png)
Binary tree related code questions [more complete] C language

Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report

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

Day20 FPGA 】 【 - block the I2C read and write EEPROM

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

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

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

【FPGA】SDRAM

leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
随机推荐
如何给网页添加icon图标?
es-head plugin insert query and conditional query (5)
Leetcode 450. 删除二叉搜索树中的节点
Day20 FPGA 】 【 - block the I2C read and write EEPROM
阿里云发布3大高性能计算解决方案
一文读懂 高性能可预期数据中心网络
What are port 80 and port 443?What's the difference?
LeetCode刷题第16天之《239滑动窗口最大值》
1815. 得到新鲜甜甜圈的最多组数 状态压缩
我的 archinstall 使用手册
直播平台开发,Flutter,Drawer侧滑
[C Language] Getting Started
「转」“搜索”的原理,架构,实现,实践,面试不用再怕了
Add PRODUCT_BOOT_JARS and classes to provide jar packages to applications
send_sig: 内核执行流程
蹭个热度-请勿打开
Object Creation and Display Transformation
Which one to choose for mobile map development?
"104 Maximum Depth of Binary Trees" in LeetCode's Day 12 Binary Tree Series
LeetCode刷题第17天之《3 无重复字符的最长子串》