From a255bdfdd24ee86eb0083c88f222b29537f994c1 Mon Sep 17 00:00:00 2001 From: Night101 <91742459+notaweed@users.noreply.github.com> Date: Sat, 2 Oct 2021 12:01:58 +0530 Subject: [PATCH] Create BubbleSort.c --- BubbleSort.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 BubbleSort.c diff --git a/BubbleSort.c b/BubbleSort.c new file mode 100644 index 0000000..222bf2f --- /dev/null +++ b/BubbleSort.c @@ -0,0 +1,58 @@ +#include + +void printArray(int* A, int n){ + for (int i = 0; i < n; i++) + { + printf("%d ", A[i]); + } + printf("\n"); +} +void bubbleSort(int *A, int n){ + int temp; + int isSorted = 0; + for (int i = 0; i < n-1; i++) // For number of pass + { + printf("Working on pass number %d\n", i+1); + for (int j = 0; j A[j+1]){ + temp = A[j]; + A[j] = A[j+1]; + A[j+1] = temp; + } + } + } +} + +void bubbleSortAdaptive(int *A, int n){ + int temp; + int isSorted = 0; + for (int i = 0; i < n-1; i++) // For number of pass + { + printf("Working on pass number %d\n", i+1); + isSorted = 1; + for (int j = 0; j A[j+1]){ + temp = A[j]; + A[j] = A[j+1]; + A[j+1] = temp; + isSorted = 0; + } + } + if(isSorted){ + return; + } + } +} + +int main(){ + int A[] = {1, 2, 5, 6, 12, 54, 625, 7, 23, 9, 987}; + int n = 11; + printf("Before Sorting: "); + printArray(A, n); // Printing the array before sorting + bubbleSort(A, n); // Function to sort the array + printf("After Sorting: "); + printArray(A, n); // Printing the array before sorting + return 0; +}