-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.c
52 lines (43 loc) · 958 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
// implementation of the bubble sort algorithm for learning purposes
int bubble_sort(int arr[], const int n);
int main(int argc, char* argv[])
{
if (argc == 1)
{
printf("Pass values to form the array\n");
return EXIT_FAILURE;
}
const int n = argc - 1;
int arr[n];
for (int i = 1; i < argc; i++)
{
arr[i - 1] = atoi(argv[i]);
}
bubble_sort(arr, n);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return EXIT_SUCCESS;
}
int bubble_sort(int arr[], const int n)
{
int upper = n - 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < upper; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
upper--;
}
return EXIT_SUCCESS;
}