-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-sort.c
52 lines (40 loc) · 953 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>
#define MAX_LEN 100
static void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
static void bubble_sort(int *array, int len)
{
int i, j;
for (i = 0; i < len - 1; i++)
for (j = 0; j < len - 1; j++)
if (array[j] > array[j + 1])
swap(&array[j], &array[j + 1]);
}
static void print_array(int *array, int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int array[MAX_LEN], len, i;
printf("What's the length of the array? Maximum lenght is %d\n", MAX_LEN);
scanf("%d", &len);
printf("Gimme the %d elements\n", len);
for (i = 0; i < len; i++) {
scanf("%d", &array[i]);
}
printf("Nonsorted array: ");
print_array(array, len);
bubble_sort(array, len);
printf("Sorted array: ");
print_array(array, len);
return 0;
}