From 5924389faad9b1dc345d1c9fc11ad802711f0f44 Mon Sep 17 00:00:00 2001 From: AbhiKul10 <40908684+AbhiKul10@users.noreply.github.com> Date: Mon, 7 Oct 2019 12:49:45 +0530 Subject: [PATCH] Create bubblesort.c --- bubblesort.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 bubblesort.c diff --git a/bubblesort.c b/bubblesort.c new file mode 100644 index 0000000..5736730 --- /dev/null +++ b/bubblesort.c @@ -0,0 +1,40 @@ +// Optimized bubble sort in C +#include +void bubbleSort(int arrayay[], int size) +{ + for (int step = 0; step & lt; size - 1; ++step) + { + + int swapped = 0; + for (int i = 0; i & lt; size - step - 1; ++i) + { + + if (arrayay[i] & gt; arrayay[i + 1]) + { + int temp = arrayay[i]; + arrayay[i] = arrayay[i + 1]; + arrayay[i + 1] = temp; + swapped = 1; + } + } + + if (swapped == 0) + break; + } +} +void printarrayay(int arrayay[], int size) +{ + for (int i = 0; i & lt; size; ++i) + { + printf("%d ", arrayay[i]); + } + printf("\n"); +} +int main() +{ + int data[] = {-2, 45, 0, 11, -9}; + int size = sizeof(data) / sizeof(data[0]); + bubbleSort(data, size); + printf("Sorted Array in Ascending Order:\n"); + printarrayay(data, size); +}