domingo, 24 de abril de 2011

CAPITULO VIII

Lo que no me puedo olvidar de Vectores
·         Inicialización
·         Ingresar datos
·         Mostrar datos
·         Otros algoritmos
ü  Ordenación
ü  Búsqueda

1.      Inicialización
Int V[ ] = new int [n];
Objetc  O[ ] = new Objet [n];
Vector V2 = new Vector (5)
//Import java.util.Vector;
Int V3[ ]; //solo es una referencia, no es instancia.
2.      Ingresar datos
                                                    0          1          2           V





V[0] = 25;       
Constante
                                   0          1          2          3          4         

300
200
0
0
10

V2.add (300);             
V2.add (200);
V2.add (10,4);


            Método

            V1 [i] = 30

            Variable




// Por teclado?????
Buffered = Es una memoria auxiliar, almacenamiento temporal de un proceso
BufferedRead   Val=(new BufferedReader(new InputSheauReader(System.in)));
Int  num = Integer.ParseInt (val.readLine ());
                                                           Flujo de teclado

3.      Mostrar datos          

Public static void Mostrar V(Vector x){
                For (int i = 0; i < x.size (); i++)
Método
                System.out.printLn (“ [ ” + i + “]” = + x.get( i ));
4.       Ordenación

2
1
3
5

V
                               I              j
                               1             2             3             5

                               I                              j              j
                               1             2             3             5
                                               I              j              j
                               1             2             3             5
                                                               I              j
                               1             2             3             5
                                                                              I              j




Public void Ordenar (int V [] ){
                For (int i = 0 ; i < V.length ; i++){
                               For (int i = 0; j < V.length; j++){
                                               If (V [i] < V [j]){
                                                               Int aux = V[j];
                                                               V[i] = V [j];
                                                               V[j] = aux;
}
}
}
}          

Import java.util.Arrays;
                                Arrays.Sort (V);
                                Mostrar (V);
5.       Búsqueda

2
1
9
3

V
                                                                                                                             Lineal secuencial
Elem = 9
                Return (1);

                               0             1             2             3             4

2
3
7
10
25

V2
                                                                                                                  Ordenado BBinaria
                               Elem=10                     Return (3)



                Public static int Buscar (int V [], int elem){
                               Int pos = -1;
                               For (int i = 0; i < V.length ; i++){
                                               If ( V[i] == elem)
                                               Pos=i];
}
                        Return pos;
}

            Import java.util.Arrays;

            Arrays.BinarySearch (V.elem);
           
EJERCICIOS

public class vector {

   public static void ordenar(int V[]){     
    for( int i = 0; i < V.length; i ++ ){
            for( int j = 0; j < V.length; j ++ ){
                         if(V[ i ] < V[ j ]){
                         int aux = V[ i ];
                         V[ i ] = V[j];
                         V[ j ] = aux;
                         }else if(V[ i ] > V[ j ]){
                                     int aux = V[ i ];
                                      V[ i ] = aux;
                                     V[ j ] = V[j];
      }
    }
   }
 
    }
   public static void mostrar (int V[]){    
      for(int i=0; i<V.length; i++){
            System.out.println(" [ " + i + " ] =  " + V[i]);      
     }
}
    public static void main(String[] args) {      
       int vec[]={7,9,1,3,8,2};
       System.out.println("vector desordenado");
            mostrar(vec);
            System.out.println("vector ordenado: ");
            ordenar(vec);
            mostrar(vec);                         
    }
    }

1 comentario:

  1. Trabajo corregido, ojo con la sintaxis
    BufferedRead Val=(new BufferedReader(new InputSheauReader(System.in)));

    Que es InputSheauReader???????????????

    ResponderEliminar