-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.c
More file actions
33 lines (33 loc) · 863 Bytes
/
bubbleSort.c
File metadata and controls
33 lines (33 loc) · 863 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
#include<stdio.h>
void main() {
int a[20], i, n, j, temp;
printf("Enter value of n : ");
scanf("%d", &n);
// Write the for loop to read array elements
for(i = 0; i < n; i++)
{
printf("Enter element for a[%d] : ", i);
scanf("%d", &a[i]);
}
printf("Before sorting the elements in the array are\n");
// Write the for loop to display array elements before sorting
for(i = 0; i < n; i++)
printf("Value of a[%d] = %d\n", i, a[i]);
//Write the code to sort elements
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("After sorting the elements in the array are\n");
// Write the for loop to display array elements after sorting
for(i = 0; i < n; i++)
printf("Value of a[%d] = %d\n", i, a[i]);
}