C/Visual C - AYUDA CON CODIGO EN C++

 
Vista:

AYUDA CON CODIGO EN C++

Publicado por tomuer01 (1 intervención) el 05/07/2007 22:58:24
Tengo problemas con los metodos de ordenamiento, no me ordena como debiera...Alguien que lo compile y me ayude a encontrar el error...
Gracias...

#include<stdio.h>
#include<conio.h>

#define N 10

/*BURBUJA*/
void burbuja(int ARREGLO[N])
{

int x;
int z;
int aux;

x = 0;

while(x < N)
{
z = N-1;

while(z >= 0)
{

if(ARREGLO[z] < ARREGLO[z - 1])
{
aux = ARREGLO[z];
ARREGLO[z] = ARREGLO[z - 1];
ARREGLO[z - 1] = aux;
}

z--;
}

x++;
}
}

/*INSERCION*/
void insercion(int ARREGLO[N])
{

int x;
int z;
int aux;

int b;
/*int flag;*/

for(x = 1; x < N; x++)
{
aux = ARREGLO[x];
z = x - 1;
/* flag = 0;*/

while(b == 0 && z >= 0)
{

if(aux < ARREGLO[z])
{
ARREGLO[z + 1] = ARREGLO[z];
z--;
}
else

b = 1;
}

ARREGLO[z + 1] = aux;
}
}

/*SELECCION*/
void seleccion(int ARREGLO[N])
{

int i;
int j;
int min;

int aux;

i = 0;

while(i < N)
{
min = i;
j = i + 1;

while (j < N)
{
if(ARREGLO[j] < ARREGLO[i])
{
min = j;
aux = ARREGLO[i];
ARREGLO[i] = ARREGLO[min];
ARREGLO[min] = aux;
}

j++;
}

i++;
}
}

int main()
{
int ARREGLO[N], COPIA[N];
int i;
char op;

printf("Ingreso de numeros...\n");
for(i=0; i<N; i++)
{
printf("Numero %d = ", i+1);
scanf("%d", &ARREGLO[i]);
}

for(;;)
{
clrscr();
printf(" ***METODOS DE ORDENAMIENTO***\n\n");
printf(" 1.- Metodo Burbuja.\n");
printf(" 2.- Metodo Insercion.\n");
printf(" 3.- Metodo Seleccion.\n");
printf(" 4.- Salir.\n");
printf(" \n\nEscoje tu opcion: ");

op = getche();

if(op>='1' && op<='3')
{
for(i=0; i<N; i++)
COPIA[i] = ARREGLO[i];
}

if(op=='1')
burbuja(COPIA);
else if(op=='2')
insercion(COPIA);
else if(op=='3')
seleccion(COPIA);
else if(op=='4')
break;
else
{
printf("\nOpcion invalida");
getch();
}

if(op>='1' && op<='3')
{
printf("\n\nArreglo original: ");
for(i=0; i<N; i++)
printf("%d ", ARREGLO[i]);

printf("\nArreglo ordenado: ");
for(i=0; i<N; i++)
printf("%d ", COPIA[i]);

getch();
}
}

return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:AYUDA CON CODIGO EN C++

Publicado por Eduardo Negrete (76 intervenciones) el 06/07/2007 02:03:47
Pues Solamente Encontre Que El Metodo De Insercion Era El Que Daba Problemas En La Inicializacion De La Variable b.

Saludos!!!

/*INSERCION*/
void insercion(int ARREGLO[N])
{

int x;
int z;
int aux;

int b;
/*int flag;*/

for(x = 1; x < N; x++)
{
aux = ARREGLO[x];
z = x - 1;
/* flag = 0;*/
/* Aqui Estaba El Problema */
b = 0; // Debes De Inicializarla A Cero
while(b == 0 && z >= 0)
{

if(aux < ARREGLO[z])
{
ARREGLO[z + 1] = ARREGLO[z];
z--;
}
else
b = 1;
}

ARREGLO[z + 1] = aux;
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar