-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTri_Bulles.c
More file actions
49 lines (43 loc) · 1.2 KB
/
Tri_Bulles.c
File metadata and controls
49 lines (43 loc) · 1.2 KB
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
#include <stdio.h>
#include <stdlib.h>
/**
* Sort array by bubble sort
*
* int array[] :: array to sort
* int len_array :: size of array
*
* return void
**/
void bubbleSort(int array[], int len_array)
{
/* condition to stop recursion */
//if (len_array <= 1){ return; }
int i, j; // counters i for 1st loop and j for 2nd
int swapped;// loop breaker stand as booleen
/*
* compares each pair of adjacent items and swaps them if they are in the wrong order
* search through the list is repeated until no swaps are needed, which indicates that the list is sorted
*
* array gets shorter as last values are sorted
*/
for (i = 0; i < len_array - 1; i++)
{
swapped = 0; // set swap to false
for (j = 0; j < len_array - i - 1; j++)// i stands for the total of value sorted
{
if (array[j] > array[j + 1])
{
swap(array, j, j + 1);
swapped = 1;// set swap to true
}
}
/* IF no two elements were swapped by inner loop, then break */
if (swapped == 0)
break;
}
/*
* Actually not working, infinite recursion that im unable to fix,
* replaced by for (i = 0; i < len_array - 1; i++)
*/
//bubbleSort(array, len_array-1);
}