当前位置:网站首页>Sorting, dichotomy
Sorting, dichotomy
2022-07-03 07:06:00 【L gold p】
1. Sort
Sorting means that the saved elements are stored according to certain rules
For example, the grades are sorted in descending order , Top three in the class , Just take the first three data
1.1 Bubble sort

1.2 Selection sort

public static void selectSort(int[] arr) {
int count = 0;
int change = 0;
for (int current = 0; current < arr.length - 1; current++) {
// hypothesis The current element Is the smallest
int min = current;
// current+1 Because There is no need to compare yourself
for (int i = current + 1; i < arr.length; i++) {
count++;
// Determine whether there are other elements than min It's still small
if (arr[min] < arr[i]) {
// If there is Just compare this to min Small element subscript , Assign a value to min
min = i;
change++;
}
}
// Judge At present current Elements Is it the smallest
if (min != current) {
// If the first element is not the smallest , Just transpose this element with the smallest element
int temp = arr[current];
arr[current] = arr[min];
arr[min] = temp;
}
}
System.out.println(" Select sort complete , perform "+count+" Time , transposition "+change+" Time ");
}
2. Look for the element
1. Find data , For example, you need to find one in a pile of data and return its index , No return found -1
The return value is boolean; It's just true Fake is false
Return to digital : For example, return index , If you can find it, return to the index , Return if you can't find it -1
Reference type : It is the object , Fake is null
2.1 In order to find

2.2 Two points search
// Two points search Also known as half search The demand data is in order
public static int binarySearch(int arr[], int target){
int count=0;
// 1. Determine the start and end and intermediate data
int startIndex=0;
int endIndex=arr.length-1;
int m=(startIndex+endIndex)/2;
// Cycle comparison
while(startIndex<=endIndex){
count++;
// If the target data is equal to the intermediate data , Return the index of intermediate data
if(target==arr[m]){
System.out.println(" Executed "+count+" Time ");
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 it is less than , 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
System.out.println(" Yes "+count+" Time ");
return -1;
}
边栏推荐
猜你喜欢

Jenkins

These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products

On the practice of performance optimization and stability guarantee

每日刷題記錄 (十一)

In depth analysis of reentrantlock fair lock and unfair lock source code implementation

La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

Use the jvisualvm tool ----- tocmat to start JMX monitoring

Journal quotidien des questions (11)
随机推荐
Interfaces and related concepts
Tool class static method calls @autowired injected service
Summary of remote connection of MySQL
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
Software testing learning - day one
Laravel frame step pit (I)
How to specify the execution order for multiple global exception handling classes
LeetCode
SecureCRT取消Session记录的密码
JUC forkjoinpool branch merge framework - work theft
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
golang操作redis:写入、读取kv数据
Advanced API (serialization & deserialization)
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
Sorting out the core ideas of the pyramid principle
JMeter test result output
"Baidu Cup" CTF game 2017 February, Web: blast-1
【类和对象】深入浅出类和对象
