-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.c
49 lines (47 loc) · 936 Bytes
/
bubblesort.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
#include <stdio.h>
void print(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return;
}
void bubblesort(int *arr, int size)
{
int swap;
for (int i = 0; i < size - 1; i++)
{
swap = 0;
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j + 1] < arr[j])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swap = 1;
}
}
if (swap == 0)
break;
}
}
int main()
{
int size;
printf("Enter size:\n");
scanf("%d", &size);
int arr[size];
printf("Enter data:\n");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Original Array:\n");
print(arr, size);
bubblesort(arr, size);
printf("Sorted Array:\n");
print(arr, size);
}