#include #include #include #if defined DJGPP || defined __TURBOC__ || \ defined __BORLAND__ || defined __LCC__ #define __CONIO_SOPORTADO__ #endif #ifdef __CONIO_SOPORTADO__ #include #define PAUSA() \ printf ("Presione una tecla para volver al men�..."); getch() #define LEER_TECLA() getch() #define LIMPIAR_PANTALLA() clrscr() #elif defined __MINGW32__ #define LEER_TECLA() getchar() #define PAUSA() system("PAUSE") #define LIMPIAR_PANTALLA() system("CLS") #else #define LEER_TECLA() getchar() #define PAUSA() getchar() #define LIMPIAR_PANTALLA() #endif #define TAM 5 #define SALIR '12' int suma=0,m=0,g=0; float promedio=0; void menu(void); int leer_opcion(void); int generar_elementos (int arreglo[]); void mostrar (int arreglo[]); void burbuja (int arreglo[]); void seleccion (int arreglo[]); int menor (int arreglo[], int desde); void insercion (int arreglo[]); void quicksort (int arreglo[]); void ord_rap (int arreglo[], int inf, int sup); void copiar_arr (int *desde, int *hacia); int main() { int arreglo[TAM]; int copia_arreglo[TAM]; char opcion = 0; while (opcion != SALIR) { menu(); opcion = leer_opcion(); switch (opcion) { case '1': generar_elementos(arreglo); copiar_arr(arreglo, copia_arreglo); LIMPIAR_PANTALLA(); printf ("\n\n\tSe generaron los siguientes elementos:\n"); mostrar (arreglo); break; case '2': LIMPIAR_PANTALLA(); printf ("\n\n\tLos elementos del arreglo son:\n"); mostrar (arreglo); break; case '3': burbuja (arreglo); copiar_arr(copia_arreglo, arreglo); break; case '4': seleccion (arreglo); copiar_arr(copia_arreglo, arreglo); break; case '5': insercion (arreglo); copiar_arr(copia_arreglo, arreglo); break; case '6': quicksort (arreglo); copiar_arr(copia_arreglo, arreglo); break; case '7': LIMPIAR_PANTALLA(); printf("Suma es=%d\n",suma); PAUSA(); break; case '8': LIMPIAR_PANTALLA(); printf("Promedio es=%d\n",promedio); PAUSA(); break; case 'A': LIMPIAR_PANTALLA(); printf ("Menor = %d\n",m); PAUSA (); break; case 'B': LIMPIAR_PANTALLA(); printf ("Mayor= %d\n",g); PAUSA (); break; } } return 0; } void menu(void) { LIMPIAR_PANTALLA(); printf ("\n\n\t Menu:\n"); printf ("\n\t1) Iniciar a introducir numeros."); printf ("\n\t2) Mostrar los elementos."); printf ("\n\t3) Ordenamiento burbuja."); printf ("\n\t4) Ordenamiento por selecci�n."); printf ("\n\t5) Ordenamiento por inserci�n."); printf ("\n\t6) Ordenamiento rapido ."); printf ("\n\t7) Suma"); printf ("\n\t8) Promedio"); printf ("\n\tA) Menor."); printf ("\n\tB) Menor."); printf ("\n\t9) Salir."); printf ("\n\n\t Ingrese su opci�n por favor: "); } int leer_opcion(void) { char opcion; opcion = LEER_TECLA(); while (opcion < '1' || opcion > SALIR) { opcion = LEER_TECLA(); } return opcion; } int generar_elementos (int arreglo[]) { int i,x; for (i=0; i arreglo[j+1]) { /* Intercambiamos */ temp = arreglo[j]; arreglo[j] = arreglo[j+1]; arreglo[j+1] = temp; } LIMPIAR_PANTALLA(); printf ("\n\n\tOrdenamiento burbuja."); printf ("\n\tEl arreglo ordenado es:\n"); mostrar (arreglo); } void seleccion (int arreglo[]) { int i; int temp, pos_men; for (i=0; i temp) && (j >= 0) ) { arreglo[j+1] = arreglo[j]; j--; } arreglo[j+1] = temp; } LIMPIAR_PANTALLA(); printf ("\n\n\tOrdenamiento por inserci�n."); printf ("\n\tEl arreglo ordenado es:\n"); mostrar (arreglo); } void quicksort (int arreglo[]) { ord_rap (arreglo, 0, TAM - 1); LIMPIAR_PANTALLA(); printf ("\n\n\tOrdenamiento r�pido (QUICKSORT)."); printf ("\n\tEl arreglo ordenado es:\n"); mostrar (arreglo); } void ord_rap (int arreglo[], int inf, int sup) { int elem_div = arreglo[sup]; int temp ; int i = inf - 1, j = sup; int cont = 1; if (inf >= sup) return; while (cont) { while (arreglo[++i] < elem_div); while (arreglo[--j] > elem_div); if (i < j) { temp = arreglo[i]; arreglo[i] = arreglo[j]; arreglo[j] = temp; } else cont = 0; } temp = arreglo[i]; arreglo[i] = arreglo[sup]; arreglo[sup] = temp; ord_rap (arreglo, inf, i - 1); ord_rap (arreglo, i + 1, sup); } void copiar_arr (int *desde, int *hacia) { int i; if ( (desde == NULL) || (hacia == NULL) ) return; for (i=0; i