<commit_before>/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // check if n even makes sense
    if (n <= 0)
    {
        return false;
    }
    // implementing a linear search algorithm
    for (int i = 0; i < n; i++)
    {
        if (values[i] == value)
        {
            return true;
        }
    }
    // not found
    return false;
}

/**
 * Sorts array of n values.<commit_msg>pset3: implemented non-recursive binary search<commit_after>/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
int findMidPoint(int min, int max)
{
    return (min + max) / 2;
}

/**
 * Non-recursive binary search
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    int min = 0;
    int max = n - 1;
    int midpoint;
    while (min <= max)
    {
        midpoint = findMidPoint(min, max);
        
        if (value < values[midpoint])
        {
            // go LEFT (modify max)
            max = midpoint - 1;
        }
        else if (values[midpoint] < value)
        {
            // go RIGHT (modify min)
            min = midpoint + 1;
        }
        else
        {
            // found element
            return true;
        }
    }
    return false;
}

/**
 * Sorts array of n values.
{'fraction_non_alphanumeric': 0.11814345991561181, 'fraction_numerical': 0.012055455093429777, 'mean_word_length': 1.6688102893890675, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1753958', 'n_tokens_mistral': 549, 'n_tokens_neox': 529, 'n_words': 186}