-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06.BubbleSort.c
More file actions
41 lines (39 loc) · 867 Bytes
/
06.BubbleSort.c
File metadata and controls
41 lines (39 loc) · 867 Bytes
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
/*
6. Develop an algorithm, implement and execute a C program that reads N integer numbers and
arrange them in ascending order using Bubble Sort.
*/
#include <stdio.h>
void main()
{
int n, i, j, a[10], temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Original Elements are\n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
for (i = 1; i < n; i++)
{
for (j = 0; j < n - i; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("\nThe sorted elements are\n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return;
}