From d05e9d0b03fcf1a6f0d4ce850f9ced811ff0fbb8 Mon Sep 17 00:00:00 2001 From: Chalani Hansika <96167664+Chalani98@users.noreply.github.com> Date: Thu, 27 Oct 2022 22:24:51 +0530 Subject: [PATCH] Create bubblesort.c --- bubblesort.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 bubblesort.c diff --git a/bubblesort.c b/bubblesort.c new file mode 100644 index 0000000..8cf041e --- /dev/null +++ b/bubblesort.c @@ -0,0 +1,37 @@ +#include + +void print(int a[], int n) // function to print array elements +{ + int i; + for (i = 0; i < n; i++) + { + printf("%d ", a[i]); + } +} +void bubble(int a[], int n) // function to implement bubble sort +{ + int i, j, temp; + for (i = 0; i < n; i++) + { + for (j = i + 1; j < n; j++) + { + if (a[j] < a[i]) + { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + } + } +} +void main() +{ + int i, j, temp; + int a[5] = {12, 37, 35, 16, 28}; + int n = sizeof(a) / sizeof(a[0]); + printf("Before sorting array elements are - \n"); + print(a, n); + bubble(a, n); + printf("\nAfter sorting array elements are - \n"); + print(a, n); +}