-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble_sorting.c
39 lines (36 loc) · 961 Bytes
/
Bubble_sorting.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
#include <stdio.h>
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
void bubbleSort(int *A, int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
int main()
{
int A[] = {5, 4, 3, 2, 1};
int n = sizeof(A) / sizeof(A[0]);
printf("Original array: ");
printArray(A, n);
bubbleSort(A, n);
printf("Sorted array: ");
printArray(A, n);
return 0;
}