-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.c
66 lines (55 loc) · 826 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
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARR_SIZE 10
int swap(int *a, int *b)
{
int temp;
int swapped = 0;
if(*a > *b)
{
temp = *a;
*a = *b;
*b = temp;
swapped = 1;
}
return swapped;
}
int main(void)
{
srand(time(NULL));
int arr[ARR_SIZE];
int *a_ptr;
a_ptr = arr;
for(int i = 0; i < ARR_SIZE; i++)
{
*a_ptr = rand()%10;
a_ptr++;
}
a_ptr = arr;
printf("BEFORE SORT: [ ");
for(int i = 0; i < ARR_SIZE; i++)
{
printf("%d, ", *a_ptr);
a_ptr++;
}
printf(" ]\n");
for(int i = 0; i < ARR_SIZE; i++)
{
a_ptr = arr;
for(int j = 0; j < (ARR_SIZE - 1) - i; j++)
{
swap(a_ptr, a_ptr+1);
a_ptr++;
}
}
a_ptr = arr;
printf("AFTER SORT: [ ");
for(int i = 0; i < ARR_SIZE; i++)
{
printf("%d, ", *a_ptr);
a_ptr++;
}
printf(" ]\n");
return 0;
}