In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java binary search method how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java binary search method how to use" it!
Title
Use the binary search method to find the elements of a specific keyword. The user is required to enter the length of the array, that is, the data length of the ordered table, and enter the array elements and keywords to find. Whether the search for the program output is successful or not, and the position of the keyword in the array if it is successful. For example, look for elements with keyword 89 in ordered tables 11, 13, 18, 28, 39, 56, 69, 89, 98, 122.
1. Program analysis
Binary search is half-and-half search, its basic idea is: first select the record in the middle of the table, compare its keyword with the given keyword key, if the key value is equal, the search is successful; if the key value is larger than the key value, then the element to be found must be in the right subtable, then continue to search the right subtable in half If the key value is smaller than the key value, the element you are looking for must be in the left child table, and continue to search the left child table in half. This is recursive until the lookup succeeds or fails (lookup scope is 0).
2. Program realization
/ * Topic: use binary search method to find the elements of a specific keyword. * File Name: BinarySearch.c * Author: Jack Cui * Created: 6 April 2016 * / # include # include / * merge sort function statement * / void BinarySearch (int iKey,int * pArr,int iNum); void main (void) {int iKeyGrader Printf ("Please enter array length:\ n"); scanf ("% d", & iNum); printf ("Please enter array elements:\ n"); pArr = (int *) malloc (sizeof (int) * iNum); for (I = 0 miti < iNum;i++) scanf ("% d", & pArr [I]); printf ("Please enter the element you are looking for:\ n") Scanf ("% d", & iKey); BinarySearch (iKey,pArr,iNum) } / * * function name: BinarySearch* parameter description: the number that iKey is looking for * * pArr array * iNum array size * description: binary search * * * / void BinarySearch (int iKey Int * pArr,int iNum) {int iLeft,iRight,iMid,iCount,iFlag ICount = 0; / / number of record lookups iFlag = 0; / / find the correct flag bit iLeft = 0; / / left minimum iRight = iNum-1; / / right maximum while (iLeft pArr [iMid]) iLeft = iMid + 1 Else if (iKey = = pArr [iMid]) {printf ("found successfully! \ nLookup% d times! PArr [% d] =% d\ n ", iCount,iMid,iKey); iFlag = 1 position breakout;}} if (iFlag = = 0) printf (" find failed! \ n ");}
3. The result shows that
Thank you for your reading, the above is the content of "how to use the Java binary search method". After the study of this article, I believe you have a deeper understanding of how to use the Java binary search method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.