当前位置:网站首页>Learning history of C language

Learning history of C language

2020-11-09 12:53:19 osc_mra0q9h6

Learning goals :

It's hard for us to understand , Let's look at the simple ones first , come on. L To rush !

Learning time :

2020 year 11 month 1 Japan

Learning output :

c Language dichotomy to achieve data search

#include<stdio.h>
/******************
   Function implementation dichotomy 
*******************/
int binary_research(int arr[],int length,int element)
{
   
   
	int left = 0;
	int right = 0;
	right = length - 1; 
	while (left <= right) {
   
   	
		int mid = (left + right) / 2;
		if (arr[mid] > element) {
   
   
			right = mid - 1;
		} else if {
   
   
		    (arr[mid] < element)
			left = mid + 1;
		} else {
   
   
			return mid;
		}
	}
	return -1;
}

int main()
{
   
   
	int numStr[] = {
   
   4, 7, 8, 45, 64, 123, 564, 586, 614, 688, 999};
	int left = 0;
	int right = 0;
	int mid = 0;
	int checkNum = 0;
	int numLen = 0;

	printf(" The string to look up is :\nnumStr[] = {4, 7, 8, 45, 64, 123, 564, 586, 614, 688, 999}\n");
	scanf("%d", &checkNum);

	numLen = sizeof(numStr) / sizeof(int);

	//mid = binary_research(numStr, numLen, checkNum);

	right = numLen - 1;
	mid = (left + right) / 2;

	//  Common method to achieve two distribution search 
	while (left <= right) {
   
   
		if (numStr[mid] > checkNum) {
   
   
			right = mid -1;
		} else if (numStr[mid] < checkNum) {
   
   
			left = mid + 1;
		} else {
   
   
			break;
		}
		mid = (left + right) / 2;
	}
	
	printf("mid = %d\n", mid);

	return 0;
}

版权声明
本文为[osc_mra0q9h6]所创,转载请带上原文链接,感谢