Bubblesort v jazyce C
Moderátor: Moderátoři
Bubblesort v jazyce C
jsem začátečník v programování v jazyce C. Dostal jsem za úkol napsat program pro "bublinkové seřazení" osmi čísel v poli od nejmenšího po nějvětší. Program mám napsaný, ale nemůžu ho doladit. Prosím dokážete někdo najít chybu a poradit? Díky moc.
Můj program:
#include <stdio.h>
int main()
{
int pole[] = {8, 4, 2, 6, 10, 16, 14, 12};
int pocet_prvku = sizeof(pole);
int i, j, pom;
printf("Vypis pole: \n");
for (i = 0; i < pocet_prvku; i++)
{
printf("%d", pole[i]);
}
for (j = (pocet_prvku - 1); j > 0; j--)
{
for (i = 0; i < pocet_prvku; i++)
{
if (pole[i-1] > pole [i])
{ pom = pole [i - 1];
pole [i - 1] = pole [i];
pole [i] = pom;
}
}
printf("\n");
}
return 0;
}
int main()
{
int pole[] = {8, 4, 2, 6, 10, 16, 14, 12};
int pocet_prvku = sizeof(pole)/sizeof(int);
int i, j, pom;
printf("Vypis pole:\n");
for (i = 0; i < pocet_prvku; i++)
{
printf("%d\n", pole[i]);
}
for (j = (pocet_prvku - 1); j >= 0; j--)
{
for (i = 0; i < pocet_prvku-1; i++)
{
if (pole[i+1] < pole [i])
{
pom = pole [i + 1];
pole [i + 1] = pole [i];
pole [i] = pom;
}
}
printf("\n");
}
printf("Vypis pole:\n");
for (i = 0; i < pocet_prvku; i++)
{
printf("%d\n", pole[i]);
}
return 0;
}
http://shop.ben.cz/cz/110120-ucebnice-j ... 1-dil.aspx
Internetový tutoriály:
http://www.sallyx.org/sally/c/
http://www.linuxsoft.cz/article.php?id_article=370
http://cs.wikipedia.org/wiki/C_(programovac%C3%AD_jazyk)