Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * helpers.c
- *
- * 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)
- {
- int min = 0;
- int max = n-1;
- if(binary(value,values,min,max) == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool binary(int value, int values[], int min, int max)
- {
- int current_index = ((min+max)/2);
- if(min > max)
- {
- return false;
- }
- else if(values[current_index] == value)
- {
- return true;
- }
- else if(values[current_index] < value)
- {
- min = current_index + 1;
- return binary(value,values,min,max);
- }
- else if(values[current_index] > value)
- {
- max = current_index - 1;
- return binary(value,values,min,max);
- }
- else
- {
- return false;
- }
- }
- /**
- * Sorts array of n values.
- */
- void sort(int values[], int n)
- {
- int temp;
- int max_value = 0;
- int s = 0;
- //Find Max Value
- for(int j = 0;j < n;j++)
- {
- if(values[j] > max_value){
- max_value = values[j];
- }
- }
- //Fill Arrays with proper counts
- int array_holder[max_value];
- for(int c = 0;c < max_value;c++)
- {
- array_holder[c]=0;
- }
- for(int i = 0;i < n;i++)
- {
- temp = values[i];
- array_holder[temp] = array_holder[temp]+1;
- }
- for(int k = 0; k <= max_value; k++)
- {
- if(array_holder[k] == 0)
- {
- k++;
- }
- while(array_holder[k] > 0)
- {
- values[s] = k;
- array_holder[k] = array_holder[k] - 1;
- s++;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement