forked from anish2210/hacktober2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble_sort.c
51 lines (51 loc) · 1.02 KB
/
Bubble_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
int main()
{
int i, n, sort;
int arr[100];
printf("Enter Number of elements\n");
scanf("%d", &n);
if (n == 0)
{
printf("No elements can be added");
return 0;
}
printf("Enter array elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Unsorted array elements=\n");
for (i = 0; i < n; i++)
{
printf("%d\t", arr[i]);
}
for (int j = 0; j < n - 1; j++)
{
sort = 1;
for (i = 0; i < n - 1 - j; i++)
{
if (arr[i] > arr[i + 1])
{
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
sort = 0;
}
}
if (sort)
{
printf("\nAlready sorted");
break;
}
}
if (!sort)
{
printf("\nSorted array elements=\n");
for (i = 0; i < n; i++)
{
printf("%d\t", arr[i]);
}
}
return 0;
}