Ejercicio 33 - Algoritmo ordenamiento


/ Published in: Pascal
Save to your folder(s)

Técnicas de Programación. Ejercicio 33 - Algoritmo selección (usando un array auxiliar)


Copy this code and paste it in your HTML
  1. {
  2. EJERCICIO 33 - PSEUDOCODIGO
  3. Algoritmo selección
  4.  
  5. INICIO
  6. Leer V1, M, N
  7.  
  8. Desde (J=1 hasta N) hacer
  9.   MENOR <-- 1
  10. Desde (I= 1 hasta N) hacer
  11. Si (V1(I) < V1(MENOR))
  12. Entonces MENOR <-- I
  13. Fin si
  14. Fin desde
  15. V2(J) <-- 1(MENOR)
  16. V1(MENOR) <-- M
  17. Fin desde
  18. Mostrar V2
  19. FIN
  20.  
  21. }
  22. Program seleccion(output);
  23. var
  24. { Se declaran las variables que se utilizaran en el programa }
  25. v1, v2 : array[1..10] of integer;
  26. m, n, j, i, menor : integer;
  27. begin
  28.  
  29. {
  30.   Completamos el array (vector) con algunos datos arbitrarios
  31.   Las próximas lineas son equivalentes a Equivalente a Leer V1, M, N
  32. }
  33. v1[1] := 20;
  34. v1[2] := 50;
  35. v1[3] := 150;
  36. v1[4] := 25;
  37. v1[5] := 70;
  38. v1[6] := 1;
  39. v1[7] := -40; { Podemos usar valores negativos }
  40. v1[8] := 33;
  41. v1[9] := 5;
  42. v1[10] := 700;
  43.  
  44. m:= 9999; { Sabemos que el array no contienen un valor superior a 9999 }
  45. n:= 10; { Cantidad de elementos que tiene el array }
  46.  
  47. { Desde (J=1 hasta N) hacer }
  48. for j := 1 to n do
  49. begin
  50. menor := 1;
  51. { Desde (I= 1 hasta N) hacer }
  52. for i := 1 to n do
  53. begin
  54. if v1[i] < v1[menor] then
  55. begin
  56. menor := i;
  57. end;
  58. end;
  59. { Fin desde }
  60. v2[j] := v1[menor];
  61. v1[menor] := m;
  62. end;
  63. { Fin desde }
  64. {
  65.   Mostrar v2 - Simplemente iteramos el vector para mostrar los valores
  66.   en las posiciones del 1 al 10.
  67.   }
  68. writeln('Veamos si los elementos en v2 estan ordenados...');
  69. for i := 1 to n do
  70. writeln(v2[i]);
  71. end.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.