-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble_sort.c
56 lines (48 loc) · 1.07 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
#include <stdio.h>
// Predefining used funtion here
void swap(int *a, int *b);
void Bubble_Sort(int array[], int n);
// main funtion
int main()
{
int size;
printf("\nEnter size of array: ");
scanf("%d", &size);
int array[size];
printf("Enter elements of array : \n");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
// calling Bubble sort function for sorting
Bubble_Sort(array, size);
// Printing elements of array after sorting using bubble sort
printf("Array after sorting using bubble sort : \n");
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
// swap funtion
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
// sorting funtion using bubble sort
void Bubble_Sort(int array[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (array[j] > array[j + 1])
{
swap(&array[j], &array[j + 1]);
}
}
}
}