-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSort.c
55 lines (44 loc) · 911 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
50
51
52
53
54
55
#include <stdio.h>
void printNumbers(int *arr, int size);
void bubbleSort(int *arr, int size);
void swap(int *a, int *b);
int main()
{
int nums[] = {8, 10, 6, 4, 5, 7, 3, 1, 9, 2};
int size = sizeof(nums) / sizeof(nums[0]);
printf("\n|\t Bubble Sort\t\t|\n");
printf("-----------------------------------------\n");
printf("\nUnsorted array\n");
printNumbers(nums, size);
bubbleSort(nums, size);
printf("\nSorted array\n");
printNumbers(nums, size);
return 0;
}
void printNumbers(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf(" %d ", arr[i]);
}
printf("\n");
}
void bubbleSort(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j + 1] < arr[j])
{
swap(&arr[j + 1], &arr[j]);
}
}
}
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}