Ordenacion de Vectores
Sea el vector :
a) Ordenar el vector con el metodo completo de ordenacion por el metodo burbuja en java.
/**
*
* @author Rominita
*/
public class Vector {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int V [] = {2,9,1,3};
// public void OrdenarVector(int V[]){;
int tmp;
int i,j;
int n = V.length;
for(i = 1;i < n; i++){
for(j = n-1 ;j >= i; j--){
if(V[j]<V[j-1]){
tmp = V[j];
V[j] = V[j-1];
V[j-1]=tmp;
}}}
for (int l = 0;l < V.length;l++){
System.out.println(V[l]);
}
// Vector ordenar = new OrdenarVector();;
}}
b) Ordenar el vector con un metodo de la clace array
package vector;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rominita
*/
public class vectorarray {
public static void main(String[] args) {
int v[] = new int[4];
Vector v2 = new Vector(4);
v[0] = 2;
v[1] = 9;
v[2] = 1;
v[3] = 3;
v2.add(2);
v2.add(9);
v2.add(1);
v2.add(3);
System.out.print("========= Creado Por Romina Laime ==========\n");
System.out.print("============================================\n");
System.out.print("Elemtos Agregados con la Clase Vector\n");
System.out.print("============================================\n");
System.out.println(v2);
System.out.print("============================================\n");
System.out.print("Elemtos Agregados de acuerdo a su posicion\n");
System.out.print("============================================\n");
Arrays.sort(v);
for(int i = 0 ; i < v.length ; i ++)
System.out.print("["+v[i]+"]");
System.out.print("\n============================================\n");
}
}
a) Ordenar el vector con el metodo completo de ordenacion por el metodo burbuja en java.
package vector;
/**
*
* @author Rominita
*/
public class Vector {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int V [] = {2,9,1,3};
// public void OrdenarVector(int V[]){;
int tmp;
int i,j;
int n = V.length;
for(i = 1;i < n; i++){
for(j = n-1 ;j >= i; j--){
if(V[j]<V[j-1]){
tmp = V[j];
V[j] = V[j-1];
V[j-1]=tmp;
}}}
for (int l = 0;l < V.length;l++){
System.out.println(V[l]);
}
// Vector ordenar = new OrdenarVector();;
}}
b) Ordenar el vector con un metodo de la clace array
package vector;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rominita
*/
public class vectorarray {
public static void main(String[] args) {
int v[] = new int[4];
Vector v2 = new Vector(4);
v[0] = 2;
v[1] = 9;
v[2] = 1;
v[3] = 3;
v2.add(2);
v2.add(9);
v2.add(1);
v2.add(3);
System.out.print("========= Creado Por Romina Laime ==========\n");
System.out.print("============================================\n");
System.out.print("Elemtos Agregados con la Clase Vector\n");
System.out.print("============================================\n");
System.out.println(v2);
System.out.print("============================================\n");
System.out.print("Elemtos Agregados de acuerdo a su posicion\n");
System.out.print("============================================\n");
Arrays.sort(v);
for(int i = 0 ; i < v.length ; i ++)
System.out.print("["+v[i]+"]");
System.out.print("\n============================================\n");
}
}
0 comentarios:
Publicar un comentario