From 12f9d211231709fdd4204f1a2424722199f5745c Mon Sep 17 00:00:00 2001 From: anmolch24 <111361672+anmolch24@users.noreply.github.com> Date: Mon, 10 Oct 2022 11:42:50 +0530 Subject: [PATCH] Create bubble sort in C --- C/bubblesort.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 C/bubblesort.c diff --git a/C/bubblesort.c b/C/bubblesort.c new file mode 100644 index 0000000..17e26ba --- /dev/null +++ b/C/bubblesort.c @@ -0,0 +1,26 @@ +#include +void bubble_sort(int a[], int n) { + int i = 0, j = 0, tmp; + for (i = 0; i < n; i++) { // loop n times - 1 per element + for (j = 0; j < n - i - 1; j++) { // last i elements are sorted already + if (a[j] > a[j + 1]) { // swop if order is broken + tmp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = tmp; + } + } + } +} +int main() { + int a[100], n, i, d, swap; + printf("Enter number of elements in the array:\n"); + scanf("%d", &n); + printf("Enter %d integers\n", n); + for (i = 0; i < n; i++) + scanf("%d", &a[i]); + bubble_sort(a, n); + printf("Printing the sorted array:\n"); + for (i = 0; i < n; i++) + printf("%d\n", a[i]); + return 0; +}