Skip to content

Instantly share code, notes, and snippets.

@Securiteru

Securiteru/find.c Secret

Created Jul 24, 2017
Embed
What would you like to do?
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
// TODO: implement a searching algorithm
int min=0;
int max=n-1;
int mid;
printf("mid is: ");
while(min<=max)
{
mid=(min+max)/2;
if(value==values[mid])
{
return 1;
printf("%i", mid);
}
else if(value>values[mid])
{
min=mid+1;
}
else
{
max=mid-1;
}
printf("%i ", mid);
}
return 0;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// TODO: implement a sorting algorithm
int temp, x, y;
for(x=0; x<n-1 ; x++)
{
for(y=0; y<n-1; y++)
{
if(values[y]>values[y+1])
{
temp=values[y];
values[y]=values[y+1];
values[y+1]=temp;
}
}
}
for(x=0; x<n; x++)
{
printf("%i ", values[x]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment