当前位置:网站首页>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;
}
边栏推荐
- Mise en place d'un environnement de développement de fonctions personnalisées
- Laravel Web Framework
- Advanced API (byte stream & buffer stream)
- Pat grade a real problem 1166
- 3311. 最长算术
- PHP install composer
- instanceof
- centos php7.2.24升级到php7.3
- On the practice of performance optimization and stability guarantee
- RestHighLevelClient获取某个索引的mapping
猜你喜欢
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
MySQL installation
Software testing learning - the next day
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
【类和对象】深入浅出类和对象
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
Integration test practice (1) theoretical basis
JMeter test result output
随机推荐
Notes on the core knowledge of Domain Driven Design DDD
Resthighlevelclient gets the mapping of an index
"Baidu Cup" CTF game 2017 February, Web: blast-1
Advanced API (multithreading)
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
服务器如何设置多界面和装IIS呢?甜甜给你解答!
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
Reading notes of "learn to ask questions"
Win 10 find the port and close the port
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
每日刷题记录 (十一)
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
Software testing learning - the next day
crontab定时任务
Resttemplate configuration use
Thoughts on project development
php artisan