Código de Java - Métodos de Ordenamiento

1.0

Publicado el 11 de Octubre del 2020gráfica de visualizaciones de la versión: 1.0
5.416 visualizaciones desde el 11 de Octubre del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import java.io.*;
import java.util.*;
 
 
public class Ordenaciones {
	//*********************************INSERCION********************************//
 
    public static void Insercion(int[] arreglo)
    {
    	int x,j;
    	for(int i=1;i<=arreglo.length-1;i++)
      {//
    		x=arreglo[i];
    		j=i-1;
 
    		while(j>=0 && x<arreglo[j])
    		{
    			arreglo[j+1]=arreglo[j];
    			j--;
    		}
    		arreglo[j+1]=x;
       }//
    }
   //**********************************SELECCION*********************************//
    public static void Seleccion(int[] arreglo)
    {
    	int minj,minx;
    	for(int i=0;i<=arreglo.length-2;i++)
    	{
    		minj=i;
    		minx=arreglo[i];
    		for(int j=i+1;j<=arreglo.length-1;j++)
    		{
    			if(arreglo[j]<minx)
    			{
    				minj=j;
    				minx=arreglo[j];
    			}
    		}
    		arreglo[minj]=arreglo[i];
    		arreglo[i]=minx;
    	}
    }
 
    //*************************MERGE SORT O FUSION*************************************//
    private static void mergeSort(int[] a,int[] temp,int izq,int der)
    {
    	if(izq<der)
    	{
    		int centro=(izq+der)/2;
    		//System.out.println("izq="+izq+"centro="+centro+"der="+der);
    		mergeSort(a,temp,izq,centro);
    		mergeSort(a,temp,centro+1,der);
    		mezclar(a,temp,izq,centro+1,der);
    	}
    }
 
    public static void mergeSort(int[] a)
    {
    	int[] temp=new int[a.length];
    	mergeSort(a,temp,0,a.length-1);
    }
 
    private static void mezclar(int[] a,int[] aux,int posizq,int posder,int posfin)
    {
    	int finizq=posder-1;
    	int posaux=posizq;
    	int numelementos=posfin-posizq+1;
 
    	while(posizq<=finizq && posder<=posfin)
    	{
    		if(a[posizq]<a[posder])
    			aux[posaux++]=a[posizq++];
    	    else aux[posaux++]=a[posder++];
    	}
    	while(posizq<=finizq)
    		aux[posaux++]=a[posizq++];
    	while(posder<=posfin)
    		aux[posaux++]=a[posder++];
    	for(int i=0;i<numelementos;i++,posfin--)
    		a[posfin]=aux[posfin];
    }
 
    //******************************BURBUJA O BUBBLE SORT**********************//
    public static void burbuja(int[] arreglo){
    	int i,j;
    	int temp;
    	boolean sorted=false;
 
    	for(i=0;i<arreglo.length-1 && !sorted;i++)
    	{
    		sorted=true;
    		for(j=arreglo.length-1;j>i;j--){
    			if(arreglo[j]<arreglo[j-1]){
    				temp=arreglo[j];
    				arreglo[j]=arreglo[j-1];
    				arreglo[j-1]=temp;
    				sorted=false;
    			}
    		}
    	}
 
    }
 
    //*****************************Quick Sort****************************************//
    public static void quickSort(int[] a)
    {
    	quickSort(a,0,a.length-1);
    }
    private static void quickSort(int[] a,int inicio,int fin)
    {
    	int pivote;
    	int nuevo_inicio,aux;
    	if(inicio<fin)
    	{
    		pivote=a[inicio];
    		nuevo_inicio=inicio;
    		for(int i=(inicio+1);i<=fin;i++)
    		{
    			if(a[i]<=pivote)
    			{
    				nuevo_inicio=nuevo_inicio+1;
    				aux=a[i];
    				a[i]=a[nuevo_inicio];
    				a[nuevo_inicio]=aux;
    			}
    		}
    		aux=a[inicio];
    		a[inicio]=a[nuevo_inicio];
    		a[nuevo_inicio]=aux;
    		quickSort(a,inicio,nuevo_inicio);
    		quickSort(a,nuevo_inicio+1,fin);
    	}
    }
 
    //**************************METODO AUXILIAR PARA IMPRIMIR EN PANTALLA**********************************//
    public static void muestra(int[] arreglo,String cad)
    {
    	System.out.print(cad);
    	for(int i=0;i<arreglo.length;i++)
    		System.out.print(arreglo[i]+" ");
    		System.out.print("\n\n");
    }
 
    public static void main(String[] args)throws IOException
    {
    	int[] nums;
    	int[] nums2;
    	int[] nums3;
    	int[] nums4;
    	int[] nums5;
 
    	InputStreamReader entrada=new InputStreamReader(System.in);
    	BufferedReader flujoEntrada=new 	BufferedReader(entrada);
        System.out.println("|METODOS DE ORDENAMIENTO|");
        System.out.println("Ingresa la cantidad de numeros del areglo:\n");
        String cantidad=flujoEntrada.readLine();
        int n=Integer.parseInt(cantidad);
 
        nums=new int[n];
        nums2=new int[n];
        nums3=new int[n];
        nums4=new int[n];
        nums5=new int[n];
        System.out.println("INGRESA LOS DATOS DEL VECTOR:\n");
    		for(int g=0;g<n;g++)//for1 para pedir los datos
    		{
    			System.out.println("Dar numero");
    		String num=flujoEntrada.readLine();
                int m=Integer.parseInt(num);
              nums[g]=m;
              nums2[g]=m;
              nums3[g]=m;
              nums4[g]=m;
              nums5[g]=m;
            }//fin for1
 
 
           int opcion=0;
 
 
 
		do{
		System.out.println("------------------------------------------------------------------------------");
		System.out.println("SELECCIONE EL METODO QUE DESEAR UTILIZAR");
		System.out.println(" 1. INSERCION ");
		System.out.println(" 2. SELECCION");
		System.out.println(" 3. MERGE SORT");
		System.out.println(" 4. BURBUJA ");
		System.out.println(" 5. QUICK SORT ");
		System.out.println(" 6.   FIN       ");
		System.out.println("\n");
		System.out.print(" TECLEE OPCION: ");
		InputStreamReader entrada2=new InputStreamReader(System.in);
        BufferedReader flujoEntrada2=new BufferedReader(entrada2);
        String datoEntrada=flujoEntrada2.readLine();
        opcion=Integer.parseInt(datoEntrada);
        switch (opcion)
				{
				case 1:
				System.out.println("------------------------------------------------------------------------------");
				Ordenaciones.muestra(nums,"datos en desorden:");
    			Ordenaciones.Insercion(nums);
    			Ordenaciones.muestra(nums,"datos en orden \n(metodo de insercion):");
						break;
				case 2:
	   			System.out.println("------------------------------------------------------------------------------");
    			Ordenaciones.muestra(nums2,"datos en desorden:");
    			Ordenaciones.Seleccion(nums2);
    			Ordenaciones.muestra(nums2,"datos en orden \n(metodo de seleccion):");
 
						break;
				case 3:
				System.out.println("------------------------------------------------------------------------------");
    			Ordenaciones.muestra(nums3,"datos en desorden:");
    			Ordenaciones.mergeSort(nums3);
    			Ordenaciones.muestra(nums3,"datos en orden \n(metodo Merge Sort):");
 
						break;
				case 4:
				System.out.println("------------------------------------------------------------------------------");
    			Ordenaciones.muestra(nums4,"datos en desorden:");
    			Ordenaciones.burbuja(nums4);
    			Ordenaciones.muestra(nums4,"datos en orden \n(metodo de la burbuja):");
 
						break;
				case 5:
				System.out.println("------------------------------------------------------------------------------");
    			Ordenaciones.muestra(nums5,"datos en desorden:");
    			Ordenaciones.quickSort(nums5);
    			Ordenaciones.muestra(nums5,"datos en orden \n(metodo de QuickSort):");
						break;
               }//fin switch
		}while(opcion!=6);//fin do
    }//fin main
}//fin clase



Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6571