Skip to content

Commit 798d583

Browse files
authored
Add files via upload
0 parents  commit 798d583

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

BinarySearch.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "BinarySearch.h"
2+
3+
uint8_t binarySearch(uint32_t *arr, uint8_t size,uint32_t number){
4+
// check whether the array is sorted or not
5+
if(!sorted(arr, size))
6+
//if the array is not sorted it will be sorted
7+
sort(arr, size);
8+
//initializing the start, mid, and end of the array
9+
uint32_t start = 0, mid = size/2, end = size;
10+
// searching for the number given
11+
while (1){
12+
// if the number is located in the index mid, the index will be returned
13+
if(number == arr[mid]){
14+
return mid;
15+
} else if (start == mid){ //checking if the search has reached the end or not
16+
return -1; // if the start = mid --> indicates that search ended
17+
}
18+
// if the number is smaller or bigger than the number in the index of mid, modify the values of start, mid, and end.
19+
else if (number <arr[mid]){
20+
end = mid -1;
21+
mid = (start + end)/2 ;
22+
continue;
23+
} else if (number > arr[mid]){
24+
start = mid +1;
25+
mid = (start + end)/2 ;
26+
continue;
27+
}
28+
}
29+
}
30+
// function to check whether the array is sorted or not
31+
uint8_t sorted (const uint32_t *arr, uint8_t size){
32+
for(int i = 0; i<size-1; i++){
33+
if(arr[i] > arr[i+1]) return 0;
34+
}
35+
return 1;
36+
}
37+
// sorting the array if not sorted using bubble sort algorithm
38+
void sort(uint32_t *arr, uint8_t size){
39+
/*using nested loops to take each element and
40+
compare it to the rest of the elements in the array */
41+
for (int i = 0; i < size-1; i++)
42+
for (int j = 0; j < size-i-1; j++)
43+
if (arr[j] > arr[j+1])
44+
// using the swap function if the element is greater than the element after it
45+
swap(&arr[j], &arr[j+1]);
46+
}
47+
// swap function to swap elements
48+
void swap(uint32_t *num1, uint32_t *num2)
49+
{
50+
int temp = *num1;
51+
*num1 = *num2;
52+
*num2 = temp;
53+
}
54+
// function used to print the array
55+
void printArray(uint32_t *arr, uint32_t size){
56+
printf("{");
57+
for(int i = 0; i< size-1; i++)
58+
printf("%d,", arr[i]);
59+
60+
printf("%d}\n", arr[size-1]);
61+
}

BinarySearch.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
5+
typedef signed char uint8_t;
6+
typedef unsigned int uint32_t;
7+
8+
extern uint8_t binarySearch(uint32_t *arr, uint8_t size,uint32_t number);
9+
10+
extern uint8_t sorted (const uint32_t *arr, uint8_t size);
11+
12+
extern void sort(uint32_t *arr, uint8_t size);
13+
14+
extern void swap(uint32_t *num1, uint32_t *num2);
15+
16+
extern void printArray(uint32_t *arr, uint32_t size);

main.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "BinarySearch.h"
2+
3+
uint8_t arrSize(const uint32_t *arr);
4+
int main() {
5+
// initialize the program
6+
printf("\t\tWelcome\n");
7+
printf("Press 's' to start the program\n");
8+
char input= ' ';
9+
scanf(" %c", &input);
10+
// if the user wants to start the program he will press 's'
11+
if(input == 's') {
12+
// changing the input to 'r' -> reload, to reload the program if wanted after ending it
13+
input = 'r';
14+
while (input == 'r') {
15+
// options given to the user
16+
printf("Options: \n");
17+
printf("a)Search for an index of a number in the array (after automatically sorted if needed)\n"
18+
"b)Array before sort\n"
19+
"c)Array after sorted\n");
20+
scanf(" %c", &input);
21+
// initializing array of uint32_t
22+
uint32_t arr[256] = {19, 10, 8, 17, 9, 12, 14, 26, 20, 24, 29, 31, 7, 40, 35};
23+
// determining the size of the array
24+
uint8_t size = arrSize(arr);
25+
// number that he user will search for its index in the array later
26+
uint32_t number = 0;
27+
// checking the user input
28+
switch (input) {
29+
case 'b':
30+
printf("Array before sort = ");
31+
printArray(arr, size);
32+
break;
33+
case 'c':
34+
// sort the array and print it
35+
sort(arr, size);
36+
printf("Array after sort = ");
37+
printArray(arr, size);
38+
break;
39+
40+
case 'a':
41+
printf("Please enter a number to search for\n");
42+
scanf(" %d", &number);
43+
//passing the array to our function and storing the returned value in result
44+
uint32_t result = binarySearch(arr, size, number);
45+
// showing the result to the user
46+
if (result == (-1)) {
47+
printf("Number is not found in the array\n");
48+
} else {
49+
printf("Number found in index %d"
50+
"\n", result);
51+
}
52+
break;
53+
default:
54+
printf("Invalid input!\n");
55+
break;
56+
}
57+
58+
// checking whether the user wants to reload the program or not
59+
printf("Press 'r' to reload or any other key to exit\n");
60+
scanf(" %c", &input);
61+
}
62+
}
63+
return 0;
64+
}
65+
//function used to determine the size of the array
66+
uint8_t arrSize(const uint32_t *arr) {
67+
int i = 0;
68+
while(arr[i] != '\0') i++;
69+
return i;
70+
}

0 commit comments

Comments
 (0)