<commit_before>#include <stdio.h>

/* This program implements the Binary Search Algorithm
 * to find a number in an array of integers.
 */
 
 #define ARRAYSIZE 12
 
int compareTo(int, int);
int showArray(int[], int);
int find(int, int[], int);
int binarySearch(int, int[], int, int);

int main() {
	int numbers[] = {1, 2, 4, 5, 7, 8, 11, 15, 17, 33, 40, 45};
	printf("Array to find a number in: ");
	showArray(numbers, ARRAYSIZE);
	printf("\n");
	
	int i, index;
	while(1)
	{
		printf("> ");
		scanf("%i", &i);
		
		// Find number
		index = find(i, numbers, ARRAYSIZE);
		if(index != -1)
			printf("  Number %i found at %i.\n\n", i, index);
		else
			printf("  Number %i was NOT found.\n\n", i);
	}
	
	return 0;
}

// This method uses the Binary Search Algorithm
// to look for a number in the given array.
int find(int target, int numbers[], int size)
{
	return binarySearch(target, numbers, 0, size);
}

// Implementation of the Recursive Binary Search algorithm
int binarySearch(int target, int numbers[],
                 int start, int limit)
{	
	if(limit - start < 1)
		return -1;
	else {
		int mid = start + (limit - start) / 2;
		
		switch(compareTo(target, numbers[mid]))
		{
			case 1:
				return binarySearch(target, numbers, mid + 1, limit);
			case -1:
				return binarySearch(target, numbers, start, mid);
			default:
				return mid;
		}
	}
} 

// Show array of integer on Console
int showArray(int numbers[], int size)<commit_msg>Add step counter for Binary Search algorithm.<commit_after>#include <stdio.h>

/* This program implements the Binary Search Algorithm
 * to find a number in an array of integers.
 *
 * The program also counts the number of steps that
 * were required to find the number.
 */
 
 #define ARRAYSIZE 12
 
int compareTo(int, int);
int showArray(int[], int);
int find(int, int[], int);
int binarySearch(int, int[], int, int);

// Counter of steps in the binary search algorithm
int gAlgorithmStepsCounter;

int main() {
	int numbers[] = {1, 2, 4, 5, 7, 8, 11, 15, 17, 33, 40, 45};
	printf("Array to find a number in: ");
	showArray(numbers, ARRAYSIZE);
	printf("\n");
	
	int i, index;
	while(1)
	{
		printf("> ");
		scanf("%i", &i);
		
		// Find number
		index = find(i, numbers, ARRAYSIZE);
		if(index != -1)
			printf("  Number %i found at %i. (%i steps)\n\n",
			       i, index, gAlgorithmStepsCounter);
		else
			printf("  Number %i was NOT found. (%i steps)\n\n",
			       i, gAlgorithmStepsCounter);
	}
	
	return 0;
}

// This method uses the Binary Search Algorithm
// to look for a number in the given array.
int find(int target, int numbers[], int size)
{
	// Reset counter for Binary Search steps
	gAlgorithmStepsCounter = 0;
	
	return binarySearch(target, numbers, 0, size);
}

// Implementation of the Recursive Binary Search algorithm
int binarySearch(int target, int numbers[],
                 int start, int limit)
{
	// Counting algorithm stepts
	gAlgorithmStepsCounter++;
	
	// Recursive Binary Search algorithm	
	if(limit - start < 1)
		return -1;
	else {
		int mid = start + (limit - start) / 2;
		
		switch(compareTo(target, numbers[mid]))
		{
			case 1:
				return binarySearch(target, numbers, mid + 1, limit);
			case -1:
				return binarySearch(target, numbers, start, mid);
			default:
				return mid;
		}
	}
} 

// Show array of integer on Console
int showArray(int numbers[], int size)
{'fraction_non_alphanumeric': 0.13387000596302923, 'fraction_numerical': 0.018187239117471676, 'mean_word_length': 3.220125786163522, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3999866', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1090, 'n_words': 414}