README.md

December 5, 2022 ยท View on GitHub

Bubble Sort Algo

Description

It is a simple algorithm about howto sort an array of a number in chronological order

More Info

Submitted On
Byd1rtyw0rm
LevelBeginner
User Rating3.8 (15 globes from 4 users)
CompatibilityC, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
CategoryAlgorithms
WorldC / C++
Archive File

Source Code

The sorting by bubble consists in traversing an array and to interchange each couple of numbers of which first ranks above the second. Several passages on the table will be necessary (exactly items_numbers - 1)

Little exemple :
ORIGINAL ARRAY : 8 3 9 7 1

First Passage

3 8 9 7 1
3 8 9 7 1
3 8 7 9 1
3 8 7 1 9

As of the first passage, the highest number is automatically thorough with the last position. Therefore, we will not need more to check it with other numbers at the time of the next passages. In the code which will follow, variable N discharges this task. In fact, each following passage will contain each one a checking of less than preceding it.

Second Passages

3 8 7 1 9
3 7 8 1 9
3 7 1 8 9

Third Passages

3 7 1 8 9
3 1 7 8 9
Fourth Passage

1 3 7 8 9

The following algorithm sort each element of an array using the bubble sorting. The variables N and K are counter and swap is a variable being used like a temporarily contain to store a value at the time while interchangement as values between two variables.

int n, k, swap;

for (n = num - 1; n > 0; n--)
{
for (k = 0; k < n; k ++)
{
if (tab[k] > tab[k + 1])
{
swap = tab[k];
tab[k] = tab[k + 1];
tab[k + 1] = swap;
}
}
}
-d1rtw0rm