当前位置:网站首页>Sorting, dichotomy
Sorting, dichotomy
2022-07-07 12:35:00 【Xiaobai shelter】
One 、 matters needing attention
1 name :
Mandatory rules : Numbers , Underline , Case letters , Dollar symbol , The number can't start , You can't use keywords and reserved words
Non mandatory rules : Look at the text and know the meaning , Hump nomenclature
Variable name and method name , Initial lowercase user, userService
Class names are capitalized User , UserService
2 notes
3 communicate
Two 、 Sort
Sort This means that the saved elements are sorted and stored according to certain rules
such as achievement Sort in descending order , Top three in the class Just take the first three data
2.1 Bubble sort
1 Compare adjacent elements . If the first one is bigger than the second one , Just swap them .
2 Do the same for each pair of adjacent elements , From the beginning of the first couple to the end of the last couple . At this point , The last element should be the largest number .
3 Repeat the above steps for all elements , Except for the last one . 4 Keep repeating the above steps for fewer and fewer elements each time , Until there's no pair of numbers to compare .
2.2 Selection sort
1 Put the smallest one on the left and take out the first one every time , The assumption is the smallest , Then compare with the following one by one , If there is one smaller than the first , Just exchange subscripts after a round of comparison , The subscript of the smallest element has been obtained , Then put it in the front for transposition
2 Repeat this step , Until after the current element When there are no other elements End
3. Look for the element
3.1 In order to find
3.2 Two points search
/**
* Two points search , Also known as half query
*
* 1 The data must be orderly
*
* 2 Generally used for fixed data , Because of order , So adding and deleting is a little more troublesome , Also consider the element shift problem
*
* 3 Both ascending and descending are OK , Just change the algorithm judgment
*
* 4 Random query performance is good
*
* Algorithm implementation
*
* 1 Determine the beginning and end of the intermediate data
* 2 With target data and Compare intermediate data
* 3 If the target data is equal to the intermediate data , Return the index of intermediate data
* 4 If the target data is larger than the intermediate data , Then continue to search in the second half , start = middle +1, End unchanged , Then generate intermediate data
* 5 If the target data is smaller than the intermediate data , Take the first half , Initial invariance , end = middle -1, Then generate intermediate data
* 6 Repeat the above steps , If you start Greater than end Description not found , return -1
*
* @param arr
* @param target
* @return
*/
public static int binarySearch(int[] arr, int target) {
// 1 Determine the beginning and end of the intermediate data
int startIndex = 0;
int endIndex = arr.length-1;
int m = (startIndex+endIndex) /2;
// Cycle comparison
while (startIndex <= endIndex) {
// 3 If the target data is equal to the intermediate data , Return the index of intermediate data
if (target == arr[m]) {
return m;
}
// If the target data is larger than the intermediate data , Then continue to search in the second half , start = middle +1, End unchanged , Then generate intermediate data
if (target > arr[m]) {
startIndex=m+1;
}else{
// If the target data is smaller than the intermediate data , Take the first half , Initial invariance , end = middle -1, Then generate intermediate data
endIndex = m-1;
}
// Generate intermediate data
m = (startIndex+endIndex) /2;
}
// Can be implemented here , Indicates that there is no
return -1;
}
边栏推荐
- [pytorch practice] write poetry with RNN
- Hi3516 full system type burning tutorial
- TypeScript 接口继承
- leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
- SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
- 即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- SQL Lab (46~53) (continuous update later) order by injection
猜你喜欢
IPv6 experiment
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
数据库系统原理与应用教程(011)—— 关系数据库
BGP third experiment report
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
Vxlan static centralized gateway
College entrance examination composition, high-frequency mention of science and Technology
Attack and defense world - PWN learning notes
浅谈估值模型 (二): PE指标II——PE Band
随机推荐
Tutorial on principles and applications of database system (007) -- related concepts of database
Cryptography series: detailed explanation of online certificate status protocol OCSP
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
MPLS experiment
数据库系统原理与应用教程(011)—— 关系数据库
Tutorial on principles and applications of database system (009) -- conceptual model and data model
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Utiliser la pile pour convertir le binaire en décimal
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Apache installation problem: configure: error: APR not found Please read the documentation
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
leetcode刷题:二叉树23(二叉搜索树中的众数)
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
Zhimei creative website exercise
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases