forked from marcospohn/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.c
64 lines (53 loc) · 1002 Bytes
/
bubble.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
#include "types.h"
#include "stat.h"
#include "user.h"
#define N 100
#define SIZE 950
int isSorted(int *array)
{
for (int i = 1; i < SIZE; i++)
if (array[i - 1] > array[i])
return 0;
return 1;
}
void swap(int *a, int *b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
void initializeArray(int *array)
{
for (int i = 0; i < SIZE; i++)
{
array[i] = SIZE - i;
}
}
void bubble(int *array)
{
int swapped = 1;
while (swapped)
{
swapped = 0;
for (int i = 0; i < SIZE - 1; i++)
{
if (array[i] > array[i + 1])
{
swap(&array[i], &array[i + 1]);
swapped = 1;
}
}
}
}
int main(int argc, char const *argv[])
{
int mypid = getpid();
for (int i = 0; i < N; i++)
{
int array[SIZE];
initializeArray(array);
bubble(array);
}
printf(1, "BUBBLE WITH PID %d FINISHED\n", mypid);
exit();
}