Skip to content

Commit 52d39e8

Browse files
authored
Create bubble_sort.c
1 parent 33ea39f commit 52d39e8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

sort/bubble_sort.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
// BUBBLE SORT
3+
4+
#include <stdio.h>
5+
6+
7+
void bubblesort(int arr[], int size)
8+
{
9+
int i, j;
10+
for (i = 0; i < size; i++) // Function where the actual algorithm is implemented
11+
{
12+
for (j = 0; j < size - i; j++)
13+
{
14+
if (arr[j] > arr[j+1])
15+
swap(&arr[j], &arr[j+1]);
16+
17+
}
18+
}
19+
}
20+
void swap(int *a, int *b)
21+
{
22+
int temp; // Function for swapping two variables
23+
temp = *a;
24+
*a = *b;
25+
*b = temp;
26+
}
27+
int main()
28+
{
29+
int array[100], i, size;
30+
printf("How many numbers you want to sort: "); // Enter the numbers to sort
31+
32+
scanf("%d", &size);
33+
34+
printf("\nEnter %d numbers : ", size);
35+
for (i = 0; i < size; i++)
36+
scanf("%d", &array[i]);
37+
bubblesort(array, size);
38+
printf("\nSorted array is ");
39+
40+
for (i = 0; i < size; i++)
41+
printf(" %d ", array[i]);
42+
return 0;
43+
44+
}

0 commit comments

Comments
 (0)