当前位置:网站首页>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;
}
边栏推荐
- Notes on the core knowledge of Domain Driven Design DDD
- 每日刷題記錄 (十一)
- Advanced API (byte stream & buffer stream)
- [attribute comparison] defer and async
- Distributed transactions
- Win 10 find the port and close the port
- 萬卷書 - 價值投資者指南 [The Education of a Value Investor]
- PHP install the spool extension
- 2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
- JMeter test result output
猜你喜欢

Inno setup production and installation package

Interfaces and related concepts

Notes on the core knowledge of Domain Driven Design DDD

卡特兰数(Catalan)的应用场景

VMware virtual machine C disk expansion

Flask Foundation

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

PAT甲级真题1166
![Gridome + strapi + vercel + PM2 deployment case of [static site (3)]](/img/65/8d79998e96a2c74ba6e237bee652c6.jpg)
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]

每日刷題記錄 (十一)
随机推荐
Daily question brushing record (11)
Software testing learning - the next day
EasyExcel
10000小時定律不會讓你成為編程大師,但至少是個好的起點
Resttemplate configuration use
centos php7.2.24升级到php7.3
How to migrate or replicate VMware virtual machine systems
Architecture notes
Resthighlevelclient gets the mapping of an index
[Code] occasionally take values, judge blanks, look up tables, verify, etc
Abstract learning
dataworks自定义函数开发环境搭建
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
JUC forkjoinpool branch merge framework - work theft
Book recommendation~
Golang operation redis: write and read hash type data
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
深度学习参数初始化(一)Xavier初始化 含代码
Error c2017: illegal escape sequence
