forked from upesacm/Mentorship-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.c
70 lines (68 loc) · 1.56 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
int main() {
int m,n;
printf("Enter the number of rows: ");
scanf("%d", &m);
printf("Enter the number of columns: ");
scanf("%d", &n);
int a[m][n], b[m * n];
for (int i = 0; i < m * n; i++)
{
b[i] = 0;
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("Enter the elements: ");
scanf("%d", &a[i][j]);
b[i * n + j] = a[i][j];
}
}
printf("Matrix is:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
for (int i = 0; i < m * n - 1; i++)
{
for (int j = 0; j < m * n - i - 1; j++)
{
if (b[j] < b[j + 1])
{
int temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
printf("Descending order is: ");
for (int i = 0; i < m * n; i++)
{
printf("%d ", b[i]);
}
printf("\n");
for (int i = 0; i < m * n - 1; i++)
{
for (int j = 0; j < m * n - i - 1; j++)
{
if (b[j] > b[j + 1])
{
int temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
printf("Ascending order is: ");
for (int i = 0; i < m * n; i++)
{
printf("%d ", b[i]);
}
printf("\n");
return 0;
}