-
Notifications
You must be signed in to change notification settings - Fork 0
/
search+sort.c
74 lines (65 loc) · 1.39 KB
/
search+sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* 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)
{
// set min and max of the search
int min = 0;
int max = n - 1;
//binary search
while(min <= max)
{
int middle = (min + max) / 2;
if(values[middle] == value)
{
return true;
}
else if(values[middle] < value)
{
min = middle + 1;
}
else if(values[middle] > value)
{
max = middle - 1;
}
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// selection sort
for(int i = 0; i < n-1; i++)
{
// declare smallest element and its place in the array
int smallest = values[i];
int position = i;
// look through the unsorted part of the list
for( int j = i+1; j < n; j++)
{
// find the smallest element
if(values[j] < smallest)
{
smallest = values[j];
position = j;
}
}
//swap elements
int temp = values[i];
values[i] = smallest;
values[position] = temp;
}
return;
}