college/Spring-2023/CS-2124/Lab-Work-1/src/lib.c

29 lines
589 B
C
Raw Normal View History

2024-02-01 10:45:55 -06:00
int binary_search(int arr[], int m, int size) {
int low = 0;
int high = size - 1;
while (low != high) {
int mid = (low + high) / 2;
if (m == arr[mid]) {
return mid;
} else if (m > arr[mid]) {
low = mid + 1;
} else {
high = mid - 1;
};
}
return -1;
}
void bubble_sort(int a[], int n) {
int i = 0, j = 0, tmp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j+1]) {
tmp = a[j];
a[j] = a[j+1];
a[j + 1] = tmp;
}
}
}
}