It is a simple algorithm about howto sort an array of a number in chronological order
Submitted On | |
By | d1rtyw0rm |
Level | Beginner |
User Rating | 3.8 (15 globes from 4 users) |
Compatibility | C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++ |
Category | Algorithms |
World | C / C++ |
Archive File |
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