Binary search algorithm-Streamline stability

Source: Internet
Author: User
Function name: Finetab binary lookup algorithm-> check the temperature table//function function: Find the data in the table in the corresponding location table data from large to small//Entry parameters: Table address, table length, Data/exit parameter to find: no//return value: Position of data in table//***************************************/int finetab (int *a,int tablong,int data)/check
		Table data is sorted from large to small {int st,ed,m;
	
		int i;
	
		st = 0; Initialize the number of queries to be the first ed = TabLong-1;//The last number i = 0;
		if (data >= a[st]) return St;
	
		else if (data < a[ed]) return ed;
			while (St < ed) {m = (st+ed) >>1//binary to query intermediate value if (data = = A[m]) break;
			
			
			if (Data < a[m] && data > A[m+1]) break;					
			if (Data > A[m]) ed = m;	
			
			else St = m;
		if (++i > Tablong) break;	
		} if (St > ed) return 0;
return m; //*************************************//Function name: Finetab binary lookup algorithm-> check the temperature table//function function: Find the data in the table corresponding to the location table data from small to large sort//Entry parameters: Table address, table Length, data//exit parameters to find: no//return value: The position of the data in the table//***************************************/int fine1tab (int *a,int tablong,int dat
		a) {int st,ed,m;
	
		int i; St = 0;//Initialize the number of queries to be the first ed = TabLong-1;//The last number i = 0;
	
		if (data <= a[st]) return ST//is less than or equal to the first number, the query value is 0, that is, the first number else if (data >= a[ed]) return ed;/is greater than or equal to the last number, and the query is the last number while (St < ed)//loop query {m = (st+ed) >>1//binary to query intermediate value if (data = = A[m]) break;/Just the value to query, end query if (DAT A > A[m-1] && data < a[m]) break;//data between the last value between intermediate and intermediate values, the end query if (data < a[m]) ed = m;//data is less than the current intermediate value, update the most	After the range value else St = m;
		Data is greater than the current intermediate value, the update start range value if (++i > Tablong)//prevent overflow break;	
		} if (St > ed) return 0;
return m;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.