-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintSBS.c
45 lines (36 loc) · 844 Bytes
/
intSBS.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int binarySearch( int a[], int N, int X ) {
int idxOfX = -1, mid, low, high;
low = 0;
high = N - 1;
while ( (low <= high) && (idxOfX == -1) ) {
mid = (low + high) / 2;
if ( a[mid] == X ){
idxOfX = mid;
} else if ( a[mid] < X ) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return idxOfX;
}
void sort(int a[], int N)
{
int i, j, next;
for ( i = 1; i < N; ++i) {
next = a[i];
for ( j = i-1; j >= 0 && a[j] > next; --j){
a[j+1] = a[j];
}
a[j+1] = next;
}
}
int main()
{
//You can write some simple example to test out sort() and
// binarySearch()
return 0;
}