Advertisement
Guest User

burbuja.c

a guest
Nov 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NUM_ARREGLOS 10
  5.  
  6.  
  7. void verarreglo(int *ver){
  8. for(int i = 0; i != NUM_ARREGLOS; i++){
  9. printf("N%d) %d\n",i+1,ver[i]);
  10. }
  11. }
  12.  
  13. void ordenarburbuja(int *arreglos, int na){ // Ana ES = A NUM_ARREGLOS
  14. //ALGORITMO BURBUJA
  15. int temp;
  16. for(int z = 1; z < na; ++z) {
  17. for(int v = 0; v < (na - z); v++) {
  18. if(arreglos[v] > arreglos[v+1]){
  19. temp = arreglos[v];
  20. arreglos[v] = arreglos[v + 1];
  21. arreglos[v + 1] = temp;
  22. }
  23. }
  24. }
  25. }
  26.  
  27. int main(int argc, char **argv){
  28.  
  29. int arreglo[NUM_ARREGLOS];
  30.  
  31. for(int i = 0; i != NUM_ARREGLOS; i++){ // Creamos a I y pedimos que se repita el ciclo hasta que I (Ingresados) Sea igual a la cantidad de arreglos definida.
  32. printf("Ingrese el arreglo %d: ", i+1);
  33. scanf(" %d",&arreglo[i]); // Pedimos el arreglo y lo ingresamos en la posicion I..
  34. for(int z = 0; z != i; z++){ // Creamos a Z Y pedimos que se repita el for hasta que Z sea igual a I (Ingresados)
  35. if(arreglo[i] == arreglo[z]){ // COMPROBAMOS QUE EL ARREGLO INGRESADO (i) NO SEA IGUAL A NINGUNO DE LOS YA DEFINIDOS. (Z)
  36. printf("No se pueden repetir\n");
  37. i--; // Restamos uno a i para volver un ciclo antras en el for.
  38. break; //Salir del ciclo for
  39. }
  40. }
  41. }
  42.  
  43. printf("Orden ingresado:\n");
  44. verarreglo(arreglo);
  45.  
  46. printf("Orden burbuja:\n");
  47. ordenarburbuja(arreglo, NUM_ARREGLOS);
  48. verarreglo(arreglo);
  49.  
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement