forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort_2.c
46 lines (37 loc) · 852 Bytes
/
bubble_sort_2.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
#define TRUE 1
#define FALSE 0
int main()
{
int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace;
/* For example
Insertion random values in array to test
*/
for (i = 0; i < MAX; i++)
{
arraySort[i] = rand() % 101;
}
/* Algorithm of bubble methods */
while (isSort)
{
isSort = FALSE;
for (i = 0; i < MAX - 1; i++)
{
if (arraySort[i] > arraySort[i + 1])
{
changePlace = arraySort[i];
arraySort[i] = arraySort[i + 1];
arraySort[i + 1] = changePlace;
isSort = TRUE;
}
}
}
/* See if it works */
for (i = 0; i < MAX; i++)
{
printf("%d\n", arraySort[i]);
}
return EXIT_SUCCESS;
}