当前位置:网站首页>6. template for integer and real number dichotomy
6. template for integer and real number dichotomy
2022-06-23 02:29:00 【I made the night white_】
List of articles
Integer dichotomy
1. Find... In monotonically increasing sequence x perhaps x In the subsequent
Template I :
a[0]~a[n-1] It's monotonous
int bin_search(int *a, int n, int x)
{
int left = 0, right = n; // Be careful : No n-1, At this time, it is closed on the left and open on the right [0,n)
while (left < right)
{
int mid = left + (right-left)/2; //int mid = (left + right) >> 1;
if (x <= a[mid]) right = mid;
else left = mid + 1;
} // Terminate in left = right
return left; // A special case :a[n-1] < x when , return n
}
When x <= a[mid] when , explain x stay mid Left side , The new search interval is the left half ,left unchanged , to update right = mid
When x > a[mid] when , explain x stay mid To the right of , The new search interval is the right half ,right unchanged , to update left= mid + 1.
After code execution ,left= right, Both equal , That is, where the answer is . The code is very efficient , Narrow the search in half at a time , The total number of times is log2(n).
2. Find... In a monotonically increasing sequence x perhaps x The precursor of
Template II :
int bin_search2(int *a, int n, int x)
{
//a[0]~a[n-1] It's monotonous
int left = 0, right = n;
while (left < right)
{
int mid = left + (right-left + 1)/2 ;
if (x >= a[mid]) left = mid;
else right = mid - 1;
} // Terminate in left = right
return left;
}
- When x >= a[mid] when , explain x stay mid To the right of , The new search interval is the right half , therefore right unchanged , to update left= mid;
- When x < a[mid] when , explain x stay mid Left side , The new search interval is the left half , therefore left unchanged , to update right = mid-1
- You can also analyze , When x<a[mid] when , Can not write right=mid, It can lead to while() The death of the loop .
The real number is divided into two parts
边栏推荐
- The practice of traffic and data isolation in vivo Reviews
- The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
- 862. triple sorting
- How to download online printing on Web pages to local PDF format (manual personal test)
- Salesforce file (II) custom development fileUpload
- Aikuai multi dialing + load balancing overlay bandwidth
- Operate attribute chestnut through reflection
- Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
- Call rest port to implement nailing notification
- Performance test -- 14 detailed explanation of performance test report and precautions
猜你喜欢

Docker installs mysql5.7 and mounts the configuration file

Xgboost Guide

For Xiaobai who just learned to crawl, you can understand it after reading it

Custom shapes for ugui skill learning

JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation

Small knowledge points of asset

Ansible practice of Nepal graph

Quick sorting C language code + auxiliary diagram + Notes

Deep learning environment configuration (I) installation of CUDA and cudnn

Performance testing -- Interpretation and practice of 16 enterprise level project framework
随机推荐
5g core network and core network evolution
Pywebio to quickly build web applications
Performance test -- Jenkins environment construction for 15jmeter performance test
Using mock data in vite projects -vite plugin mock
Operate attribute chestnut through reflection
Log a log4j2 vulnerability handling
Interviewer: what is the difference between SSH and SSM frameworks? How to choose??
Cmake configuration error, error configuration process, Preject files may be invalid
Spread spectrum and frequency hopping
Detailed explanation of GCC usage
Markdown - mark above / below symbol (typora, latex)
Circuit analysis (circuit principle)
JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
The practice of traffic and data isolation in vivo Reviews
Learning notes of recommendation system (1) - Collaborative Filtering - Theory
Small knowledge points of asset
Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)
Buuctf misc-[bjdctf2020] Nani
How to make traditional Chinese medicine labels with pictures
1. Mx6u bare metal program (5) - external interrupt
