当前位置:网站首页>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;
}
边栏推荐
- JMeter JSON extractor extracts two parameters at the same time
- DBNet:具有可微分二值化的实时场景文本检测
- Software testing assignment - the next day
- Advanced APL (realize group chat room)
- 2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
- Daily question brushing record (11)
- Upgrade CentOS php7.2.24 to php7.3
- “百度杯”CTF比赛 2017 二月场,Web:爆破-1
- Advanced API (character stream & net for beginners)
猜你喜欢
随机推荐
Advanced APL (realize group chat room)
Upgrade CentOS php7.2.24 to php7.3
Class and object summary
What are the characteristics and functions of the scientific thinking mode of mechanical view and system view
Distributed lock
Modify MySQL password
Flask Foundation
Winter vacation work of software engineering practice
Liang Ning: 30 lectures on brain map notes for growth thinking
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
Getting started with pytest
【无标题】
Pits encountered in the use of El checkbox group
Summary of remote connection of MySQL
Jenkins
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
Golang operation redis: write and read kV data
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'?
Laravel frame step pit (I)
Troubleshooting of high CPU load but low CPU usage










