From 92e28e4ec2e7de5fc37d7b573610785a17e5fa33 Mon Sep 17 00:00:00 2001 From: Gayathri Sankar <72961594+gayathri-commits@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:07:43 +0530 Subject: [PATCH] Create bubble_sort.c --- Task2/Comp_coding/bubble_sort.c | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Task2/Comp_coding/bubble_sort.c diff --git a/Task2/Comp_coding/bubble_sort.c b/Task2/Comp_coding/bubble_sort.c new file mode 100644 index 0000000..f3b37a6 --- /dev/null +++ b/Task2/Comp_coding/bubble_sort.c @@ -0,0 +1,52 @@ +#include + +void bubbleSort(int arr[], int n) { + int temp; + int swapped; + + for (int i = 0; i < n - 1; i++) { + swapped = 0; // Initialize a flag to check if any swapping occurs in this pass + + // Perform a pass through the array + for (int j = 0; j < n - i - 1; j++) { + // Swap if the element found is greater than the next element + if (arr[j] > arr[j + 1]) { + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = 1; // Set the flag to true to indicate a swap + } + } + + // If no two elements were swapped in this pass, the array is already sorted + if (swapped == 0) { + break; + } + } +} + +int main() { + int n; + + printf("Enter the number of elements in the array: "); + scanf("%d", &n); + + int arr[n]; + + printf("Enter %d elements:\n", n); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + // Sort the array using Bubble Sort + bubbleSort(arr, n); + + printf("Sorted array in ascending order:\n"); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + + printf("\n"); + + return 0; +}