From 740cbf713a0a8b36358f03639e40ccf495abb708 Mon Sep 17 00:00:00 2001 From: kavvyaaa-a Date: Tue, 28 Oct 2025 22:25:11 +0530 Subject: [PATCH 1/3] update --- C/algorithms/searching/binary_search.c | 68 ++++++++++++++++---------- 1 file changed, 41 insertions(+), 27 deletions(-) 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 Date: Tue, 28 Oct 2025 22:32:10 +0530 Subject: [PATCH 2/3] changes --- C/algorithms/sorting/bubble_sort.c | 131 ++++++++++++++--------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/C/algorithms/sorting/bubble_sort.c b/C/algorithms/sorting/bubble_sort.c index 5086dbcc..c89bc771 100644 --- a/C/algorithms/sorting/bubble_sort.c +++ b/C/algorithms/sorting/bubble_sort.c @@ -1,79 +1,72 @@ -/* - * Algorithm: [Bubble Sort] - * Description: [It starts from the beginning of the list and compares each - * pair of adjacent elements, and swap them if they are in the - * wrong order] - * Time Complexity : - * Best Case : O(n) // when given array is already sorted. - * Average Case : O(n^2) //when given array is in random order - * Worst Case : O(n^2) // when given array is in reverse order - * Space Complexity: - * Worst : 0(1) - * Author: [tanshen-kun] - */ +// Bubble Sort Implementation in C +// Hacktoberfest 2025 Contribution #include -/* - * 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]; + + // Get input from user + inputArray(arr, size); + + printf("\nOriginal array:\n"); + displayArray(arr, size); + + // Sort array using Bubble Sort + bubbleSort(arr, size); + + printf("\nSorted array in ascending order:\n"); + displayArray(arr, size); + + 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; + int swapped; + + for (int i = 0; i < size - 1; i++) { + swapped = 0; + + 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 array is already sorted + if (!swapped) + break; + } } From 9e0fa3ff8e111a868a43c87f3a5635abd1d3f72a Mon Sep 17 00:00:00 2001 From: kavvyaaa-a Date: Tue, 28 Oct 2025 22:39:45 +0530 Subject: [PATCH 3/3] changes --- C/algorithms/sorting/bubble_sort.c | 45 +++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/C/algorithms/sorting/bubble_sort.c b/C/algorithms/sorting/bubble_sort.c index c89bc771..d11a71fa 100644 --- a/C/algorithms/sorting/bubble_sort.c +++ b/C/algorithms/sorting/bubble_sort.c @@ -1,5 +1,31 @@ -// Bubble Sort Implementation in C -// Hacktoberfest 2025 Contribution +/* +Bubble Sort Algorithm + +Description: +Bubble Sort is a simple comparison-based sorting algorithm. +It repeatedly steps through the array, compares adjacent elements, +and swaps them if they are in the wrong order. This process is repeated +until the array is completely sorted. The largest element "bubbles up" +to the end of the array with each pass. + +Approach: +- Compare adjacent elements and swap if needed. +- After each iteration, the largest unsorted element is moved to its correct position. +- Repeat the process until no swaps are needed (optimized version). + +Use Cases: +- Small datasets +- Educational purposes for learning sorting concepts + +Time Complexity: +- Best Case: O(n) when the array is already sorted +- Average Case: O(n²) +- Worst Case: O(n²) +Reason: Two nested loops — outer loop runs n times, inner loop runs (n - i - 1) times. + +Space Complexity: +- O(1): Uses only a few extra variables for swapping and flags. +*/ #include @@ -16,18 +42,23 @@ int main() { int arr[size]; - // Get input from user + // Input elements inputArray(arr, size); printf("\nOriginal array:\n"); displayArray(arr, size); - // Sort array using Bubble Sort + // 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; } @@ -49,12 +80,12 @@ void displayArray(int arr[], int size) { // Performs Bubble Sort on the array void bubbleSort(int arr[], int size) { - int temp; - int swapped; + 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 @@ -65,7 +96,7 @@ void bubbleSort(int arr[], int size) { } } - // Optimization: stop if array is already sorted + // Optimization: Stop if no swaps occurred if (!swapped) break; }