Skip to content

Commit

Permalink
Merge pull request #1 from AbhiKul10/AbhiKul10-patch-1
Browse files Browse the repository at this point in the history
bubblesort.c
  • Loading branch information
AbhiKul10 authored Oct 7, 2019
2 parents 6d480a6 + 5924389 commit b476e9a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions bubblesort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Optimized bubble sort in C
#include <stdio.h>
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);
}

0 comments on commit b476e9a

Please sign in to comment.