-
Notifications
You must be signed in to change notification settings - Fork 256
/
01 Bubble Sort.c
51 lines (44 loc) · 1003 Bytes
/
01 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>
#include <stdlib.h>
void swap(int* x, int* y) // function for swapping
{
int temp = *x;
*x = *y;
*y = temp;
}
void Bubble(int A[], int n) // size of array // no of elements
{
int i, j, flag = 0; // flag is for adptive (Criteria)
for (i = 0; i < n - 1; i++)
{
flag = 0;
for (j = 0; j < n - i - 1; j++)
{
if (A[j] > A[j + 1])
{
swap(&A[j], &A[j + 1]); // if is greater then swap the elements
flag = 1;
}
}
if (flag == 0)
break;
}
}
int main()
{
int A[20], i;
int n = sizeof(A) / sizeof(int);
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
scanf_s("%d", & A[i]);
}
Bubble(A, n); // callig the funtion
for (i = 0; i < n; i++)
printf("%d ", A[i]);
printf("\n");
return 0;
}
// Bubble sort is Adptive: if the list is sorted it will use flag // Bubble sort is stable : in case of duplicate
// time complexity is : min = o(n) - in the case of Adaptivity
// : max = o(n2)