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

Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method

《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

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

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

机器学习中什么是集成学习?

LeetCode刷题第11天字符串系列之《 58最后一个单词长度》

Provincial level of Echart maps, as well as all prefecture-level download and use
![Binary tree related code questions [more complete] C language](/img/85/a109eed69cd54be3c8290e8dd67b7c.png)
Binary tree related code questions [more complete] C language

What is ensemble learning in machine learning?
随机推荐
洛谷P4324 扭动的回文串
使用百度EasyDL实现施工人员安全装备检测
shell monitors gpu usage
这些云自动化测试工具值得拥有
拼多多店铺营业执照相关问题
Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report
洛谷P1763 埃及分数
每日一题-滑动窗口
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
Mysql中事件和定时任务
洛谷P4847 银河英雄传说V2
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
The custom of the C language types -- -- -- -- -- - structure
Leetcode 108. 将有序数组转换为二叉搜索树
AVH 动手实践 (二) | 在 Arm 虚拟硬件上部署 PP-OCR 模型
优先级队列
Enter the starting position, the ending position intercepts the linked list
校园兼职平台项目反思
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
我的 archinstall 使用手册