diff --git a/C/algorithms/searching/binary_search.c b/C/algorithms/searching/binary_search.c index 0c69ed23..c3606360 100644 --- a/C/algorithms/searching/binary_search.c +++ b/C/algorithms/searching/binary_search.c @@ -11,32 +11,46 @@ */ #include -#define SIZE 10 -int BinarySearch(int [],int); -int main() -{ - int a[SIZE]={3,5,9,11,15,17,22,25,37,68},key,pos; - printf("Enter the Search Key\n"); - scanf("%d",&key); - pos=BinarySearch(a,key); - if(pos==-1) - printf("The search key is not in the array\n"); - else - printf("The search key %d is at location %d\n",key,pos); - return 0; +//function to perform binary search +int binarySearch(int arr[], int size, int key) { + int low = 0, high = size - 1, mid; + + while (low <= high) { + mid = (low + high) / 2; + + if (arr[mid] == key) + return mid; + else if (key < arr[mid]) + high = mid - 1; + else + low = mid + 1; } - int BinarySearch (int A[],int skey) - { - int low=0,high=SIZE-1,middle; - while (low <=high){ - middle=(low+high)/2; - if(skey==A[middle]) - return middle; - else if(skey -/* - * bubble_sort : it takse the array and its size then it starts to travel array - * while comparing successive pairs of element until the travel reach the last - * element - * @param arr: Array to process - * @param size: Size of the array - * @return: None - */ - -void bubble_sort(int arr[], int size) { - for (int i = 0; i < size - 1; i++) { - for (int j = 0; j < size - i - 1; j++) { - if (arr[j] > arr[j + 1]) { - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } +// Function prototypes +void inputArray(int arr[], int size); +void displayArray(int arr[], int size); +void bubbleSort(int arr[], int size); + +int main() { + int size; + + printf("Enter the number of elements: "); + scanf("%d", &size); + + int arr[size]; + + // Input elements + inputArray(arr, size); + + printf("\nOriginal array:\n"); + displayArray(arr, size); + + // Sort array + bubbleSort(arr, size); + + printf("\nSorted array in ascending order:\n"); + displayArray(arr, size); + + // Example test cases (demonstration) + // Input: [5, 2, 9, 1, 5, 6] + // Output: [1, 2, 5, 5, 6, 9] + // Edge Case: Single element or already sorted array + + return 0; } -/* - * printArray: Prints a given array - * - *@param arr: Array to print - *@param size: Size of the array - */ -void printArray(int arr[], int size) { - for (int i = 0; i < size; i++) { - printf("%d ", arr[i]); - } - printf("\n"); +// Reads 'size' number of elements into the array +void inputArray(int arr[], int size) { + printf("Enter %d elements:\n", size); + for (int i = 0; i < size; i++) { + scanf("%d", &arr[i]); + } } -void test_algorithm() { - printf("Testing Algorithm...\n"); - - // Test Case 1 : Already Sorted - int test_arr1[] = {1, 2, 3, 4, 5}; - int size1 = sizeof(test_arr1) / sizeof(test_arr1[0]); - printf("Test 1 : "); - bubble_sort(test_arr1, size1); - printArray(test_arr1, size1); - - // Test Case 2 : Random order - int test_arr2[] = {3, 1, 5, 2, 4}; - int size2 = sizeof(test_arr2) / sizeof(test_arr2[0]); - printf("Test 2 : "); - bubble_sort(test_arr2, size2); - printArray(test_arr2, size2); - - // Test Case 3 : Reverse Order - int test_arr3[] = {5, 4, 3, 2, 1}; - int size3 = sizeof(test_arr3) / sizeof(test_arr3[0]); - printf("Test 3 : "); - bubble_sort(test_arr3, size3); - printArray(test_arr3, size3); +// Displays the contents of the array +void displayArray(int arr[], int size) { + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); } -int main() { - test_algorithm(); - return 0; +// Performs Bubble Sort on the array +void bubbleSort(int arr[], int size) { + int temp, swapped; + + for (int i = 0; i < size - 1; i++) { + swapped = 0; + + // Compare adjacent elements + for (int j = 0; j < size - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + // Swap elements + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = 1; + } + } + + // Optimization: Stop if no swaps occurred + if (!swapped) + break; + } }