Advertisement
crispm50

cris ordenacion

Sep 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 10.84 KB | None | 0 0
  1. (*-------------------------- S H E L L S O R T(METODO SHELL) DECRECIENTE ---------------------*)
  2. Procedure ShellSortDrececiente(Var X : TVEmpleados; N: Integer);
  3.           Var
  4.              Done: Boolean;
  5.              Jump,I,J: Integer;
  6.           Begin
  7.                Jump := N;
  8.                While (Jump > 1) Do
  9.                      Begin
  10.                           Jump := Jump Div 2;
  11.                           Repeat
  12.                                 Done := true;
  13.                                 For J := 1 To (N - Jump) Do
  14.                                     Begin
  15.                                          I := J + Jump;
  16.                     //AGREGAR LOS CAMPOS DEL REGISTRO QUE SE QUIERE ORDENAR > decreciente
  17.                                          If (X[J].nombre > X[I].nombre) Then //<---aqui elijo el campo a ordenar
  18.                                             Begin
  19.                                                  Swap(X[J], X[I]);
  20.                                                  Done := false
  21.                                             End;
  22.                                     End;
  23.                           Until Done;
  24.                      End;
  25.                 mostrarVector(X,N);
  26.  
  27.           End;
  28.  
  29. (*-------------------------- S H E L L S O R T(METODO SHELL) CRECIENTE ---------------------*)
  30. Procedure ShellSortCreciente(Var X : TVEmpleados; N: Integer);
  31.           Var
  32.              Done: Boolean;
  33.              Jump,I,J: Integer;
  34.           Begin
  35.                Jump := N;
  36.                While (Jump > 1) Do
  37.                      Begin
  38.                           Jump := Jump Div 2;
  39.                           Repeat
  40.                                 Done := true;
  41.                                 For J := 1 To (N - Jump) Do
  42.                                     Begin
  43.                                          I := J + Jump;
  44.                     //AGREGAR LOS CAMPOS DEL REGISTRO QUE SE QUIERE ORDENAR > decreciente
  45.                                          If (X[J].nombre > X[I].nombre) Then //<---aqui elijo el campo a ordenar
  46.                                             Begin
  47.                                                  Swap(X[J], X[I]);
  48.                                                  Done := false
  49.                                             End;
  50.                                     End;
  51.                           Until Done;
  52.                      End
  53.           End;
  54.  
  55. Procedure StrSelectSortDecreciente(Var X : TVEmpleados; N : Integer);
  56. Var
  57.    I,J,K : Integer;
  58.    Y:TEmpleado; //tiene que ser un registro
  59. Begin
  60.     //ordenacion por seleccion
  61.      For I := 1 To N - 1 Do
  62.      Begin
  63.           K := I;
  64.           Y:= x[I];//guardo un registro en Y
  65.           For J := (I + 1) To N Do
  66.               //aqui es donde tengo que elegir el campo a ordenar > DECRECIENTE
  67.               If (X[J].nombre  > Y.nombre) Then//esta la parte importante elijo el campo por el que quiero ordenar
  68.               Begin
  69.                    K := J;
  70.                    Y := X[J];
  71.               End;
  72.               X[K] := X[I];
  73.               X[I] := Y;
  74.      End;
  75.      mostrarVector(X,N);
  76. end;
  77.  
  78. Procedure StrSelectSortCreciente(Var X : TVEmpleados; N : Integer);
  79. Var
  80.    I,J,K : Integer;
  81.    Y:TEmpleado; //tiene que ser un registro
  82. Begin
  83.     //ordenacion por seleccion
  84.      For I := 1 To N - 1 Do
  85.      Begin
  86.           K := I;
  87.           Y:= x[I];//guardo un registro en Y
  88.           For J := (I + 1) To N Do
  89.               //aqui es donde tengo que elegir el campo a ordenar < CRECIENTE
  90.               If (X[J].nombre  < Y.nombre) Then//esta la parte importante elijo el campo por el que quiero ordenar
  91.               Begin
  92.                    K := J;
  93.                    Y := X[J];
  94.               End;
  95.               X[K] := X[I];
  96.               X[I] := Y;
  97.      End;
  98.      mostrarVector(X,N);
  99. end;
  100.  
  101. **********************************************************
  102.  
  103.  
  104.  
  105.  
  106.  
  107. procedure insertarPersona(var v: t_vectorPer;var n:Integer);
  108. var
  109.    posLegB,legajoB,pos,i:integer;
  110. begin
  111.      WriteLn('ingrese el legajo a insertar: ');
  112.      ReadLn(legajoB);//legajo a buscar
  113.  
  114.  
  115.      posLegB:= BuscarPosPersona(v,n,legajoB);
  116.      //si posLegB = a un numero --> ya existe el legajo osea repetido
  117.      //si posLegB = 0 -.>no existe el legajo portanto puedo insertar.
  118.      while(posLegB<>0) do
  119.      begin
  120.         WriteLn('***el legajo ya exite **');
  121.         WriteLn('ingrese el legajo a insertar: ');
  122.         ReadLn(legajoB);
  123.         posLegB:= BuscarPosPersona(v,n,legajoB);
  124.      end;
  125.  
  126.      WriteLn('ingrese una posicion a insertar');
  127.      ReadLn(pos);
  128.  
  129.      for i:=n downto pos do
  130.       begin
  131.            v[i+1]:=v[i];
  132.       end;
  133.  
  134.      //empiezo a insertar el nombre, sector
  135.      with v[pos] do
  136.      begin
  137.           legajo:=legajoB;
  138.           WriteLn('Ingrese un nombre a insertar ');
  139.           ReadLn(nombre);
  140.           WriteLn('Ingrese el sector a insertar');
  141.           ReadLn(sector);
  142.      end;
  143.  
  144.      WriteLn('***insercion exitosa***');
  145.       n:=n+1;  //aumento n xq se inserto un registro mas
  146. end;        
  147. **************************************************
  148.  
  149. Procedure StrSelectSort(Var X : t_vectorPer; N : Integer);
  150. Var
  151.    I,J,K : Integer;
  152.    Y:RPersona;
  153. Begin
  154.     //ordenacion por seleccion
  155.      For I := 1 To N - 1 Do
  156.      Begin
  157.           K := I;
  158.           Y:= x[I];//guardo un registro en Y
  159.           For J := (I + 1) To N Do
  160.               //aqui es donde tengo que elegir el campo a ordenar
  161.               // < CRECIENTE
  162.               // > DECRECIENTE
  163.               If (X[J].nombre  < Y.nombre) Then//esta la parte importante
  164.               Begin
  165.                    K := J;
  166.                    Y := X[J];
  167.               End;
  168.               X[K] := X[I];
  169.               X[I] := Y;
  170.      End;
  171.      MostrarVectorDePersonas(X,N);
  172. end;  
  173.  
  174. ***************************************************************
  175. procedure modificarPersona(var v:t_vectorPer;n:integer);
  176. var
  177.    legajoB,posLegB:integer;
  178. begin
  179.      WriteLn('ingrese el legajo a modificar: ');
  180.      ReadLn(legajoB);
  181.      posLegB:= BuscarPosPersona(v,n,legajoB);
  182.  
  183.      while(posLegB=0) do
  184.      begin
  185.         WriteLn('***el legajo no exite **');
  186.         WriteLn('ingrese el legajo a modificar: ');
  187.         ReadLn(legajoB);
  188.         posLegB:= BuscarPosPersona(v,n,legajoB);
  189.      end;
  190.  
  191.      with v[poslegB] do
  192.      begin
  193.           legajo:=legajoB;
  194.           WriteLn('Ingrese un nombre');
  195.           ReadLn(nombre);
  196.           WriteLn('Ingrese el sector');
  197.           ReadLn(sector);
  198.      end;
  199.      WriteLn('***modificacion exitosa***');
  200. end;    
  201. ***********************************************************************
  202.  
  203.  
  204. ********************************************************
  205. Procedure StrInsert(Var X : tvector; N : Integer);
  206.           Var
  207.              K,J: Integer;
  208.              Found: Boolean;
  209.           Begin
  210.                For J := 2 To N Do
  211.                    Begin
  212.                         K := J - 1;
  213.                         Found := false;
  214.                         While (K >= 1) And (Not Found) Do
  215.                               If (X[J].apellido < X[K].apellido) Then
  216.                                  Begin
  217.                                       X[K + 1] := X[K];
  218.                                       K := K - 1
  219.                                  End
  220.                               else
  221.                                   Found := true;
  222.                                   X[K + 1] := X[J];
  223.                    End
  224.           End;              
  225. **************************************************************
  226.           Procedure ShellSort(Var V : tvector; N : Integer);
  227.           Var
  228.              Done: Boolean;
  229.              Jump,I,J : Integer;
  230.           Begin
  231.                Jump := N;
  232.                While (Jump > 1) Do
  233.                      Begin
  234.                           Jump := Jump Div 2;
  235.                           Repeat
  236.                                 Done := true;
  237.                                 For J := 1 To (N - Jump) Do
  238.                                     Begin
  239.                                          I := J + Jump;
  240.                                          If (V[J].apellido > V[I].apellido) Then
  241.                                             Begin
  242.                                                  Swap(V[J], V[I]);
  243.                                                  Done := false
  244.                                             End;
  245.                                     End;
  246.                           Until Done;
  247.                      End
  248.           End;
  249.                                        
  250. ********************************************************
  251. function BusquedaSecuencial(v: t_vectorPer;n:Integer; legajoBus:integer):boolean;
  252. var
  253.    bandB:boolean;
  254.    i:Integer;
  255. begin
  256.      bandB:=false;
  257.      for i:=1 to n do
  258.      begin
  259.        if(v[i].legajo=legajoBus) then
  260.        begin
  261.             bandB:= true;
  262.        end
  263.      end;
  264.      BusquedaSecuencial:=bandB;
  265. end;
  266. ************************************************************************
  267. function BusquedaBinario(V:t_vectorPer; N:Integer;elem:integer):integer;
  268. var
  269.    posInicial, posFinal, medio:integer;
  270. begin
  271.      posInicial := 1;
  272.      posFinal := N;
  273.      BusquedaBinario := 0;
  274.      repeat
  275.            medio:=(posInicial+posFinal)div 2;
  276.            if elem=V[medio].Legajo then
  277.               BusquedaBinario := medio
  278.            else
  279.                 //aqui va porque campo quiero buscar
  280.                if elem<V[medio].Legajo then
  281.                   posFinal:=medio-1
  282.                else
  283.                    posInicial:=medio+1;
  284.  
  285.      until (posInicial>posFinal) or (BusquedaBinario<>0);
  286. end;
  287.    
  288. ************************************************************************************
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296. program Ordenacion;
  297.  
  298. {$mode objfpc}{$H+}
  299.  
  300. uses crt;
  301. type
  302.   TReg = Record
  303.     cod: char;
  304.     precio: real;
  305.   end;
  306.   TArrReg = array[1..10]of TReg;
  307.  var
  308.    L : TArrReg;
  309.    R : TReg;
  310.    N : integer;
  311.  procedure cargarVector(var L: TArrReg; var N: Integer);
  312.  var
  313.    i: integer;
  314.  begin
  315.    write('Ingrese la cantidad de datos: ');
  316.    readln(N);
  317.    for i := 1 to N do
  318.     begin
  319.       write('Ingrese codigo: ');
  320.       readln(L[i].cod );
  321.       write('Ingrese precio: ');
  322.       readln(L[i].precio );
  323.     end;
  324.  end;
  325. procedure mostrarVector(L: TArrReg; N: Integer);
  326. var
  327.   i: integer;
  328. begin
  329.   for i := 1 to N do
  330.    begin
  331.      writeln('Codigo: ', L[i].cod);
  332.      writeln('Precio: ', L[i].precio:7:2);
  333.    end;
  334. end;
  335. Procedure Swap(Var X, Y : TReg);
  336. Var
  337.    Temp: TReg;
  338. Begin
  339.   Temp:= X;
  340.   X:= Y;
  341.   Y:= Temp
  342. End;
  343. Procedure BubbleSort(Var X : TArrReg; N : Integer);
  344. Var
  345.    I, J : Integer;
  346. Begin
  347.   For I := 2 To N Do
  348.   Begin
  349.     For J := N Downto I Do
  350.       If (X[J].cod > X[J - 1].cod) Then
  351.         Swap(X[J - 1], X[J]);
  352.   End
  353. End;
  354. begin
  355.   writeln('Bienvenido a mi programa...');
  356.   cargarVector(L, N);
  357.   mostrarVector(L,N);
  358.   BubbleSort(L,N);
  359.   readkey;
  360.   writeln('Los datos ordenados son');
  361.   mostrarVector(L,N);
  362.   readkey;
  363. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement