forked from gunanksood/C-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.c
More file actions
40 lines (37 loc) · 801 Bytes
/
bubblesort.c
File metadata and controls
40 lines (37 loc) · 801 Bytes
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
// Optimized bubble sort in C
#include <stdio.h>
void bubbleSort(int arrayay[], int size)
{
for (int step = 0; step & lt; size - 1; ++step)
{
int swapped = 0;
for (int i = 0; i & lt; size - step - 1; ++i)
{
if (arrayay[i] & gt; arrayay[i + 1])
{
int temp = arrayay[i];
arrayay[i] = arrayay[i + 1];
arrayay[i + 1] = temp;
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
void printarrayay(int arrayay[], int size)
{
for (int i = 0; i & lt; size; ++i)
{
printf("%d ", arrayay[i]);
}
printf("\n");
}
int main()
{
int data[] = {-2, 45, 0, 11, -9};
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printf("Sorted Array in Ascending Order:\n");
printarrayay(data, size);
}