Java - ¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

 
Vista:
sin imagen de perfil
Val: 47
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

Publicado por Pedro (25 intervenciones) el 07/03/2020 18:36:40
tengo una duda y es como podría implementar el algoritmo Quicksort para ordenar alfabeticamente una lista de nombres por ejemplo: Marcos Calderon, Matias Ruiz, Daniela Goretti, Marcos Hurtado, Marcos Phillips.

Toda ayuda se agradece
Un saludo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

Publicado por Costero (148 intervenciones) el 07/03/2020 20:02:59
En este enlace explican el algorithmo de Quicsort in ponen un ejemplo the como sortear una list the numeros.
https://javarevisited.blogspot.com/2014/08/quicksort-sorting-algorithm-in-java-in-place-example.html

Abajo yo transforme el ejemplo para nombres:

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
import java.util.Arrays;
 
/**
 * Test class to sort array of integers using Quicksort algorithm in Java.
 *
 * @author Javin Paul
 */
public class QuickSortDemoName {
 
    public static void main(String args[]) {
        // unsorted integer array
        String[] unsorted = {"Marcos Calderon", "Vatias Ruiz", "Daniela Goretti", "Marcos Hurtado", "Marcos Phillips"};
        System.out.println("Unsorted array :" + Arrays.toString(unsorted));
 
        QuickSortName algorithm = new QuickSortName();
 
        // sorting integer array using quicksort algorithm
        algorithm.sort(unsorted);
 
        // printing sorted array
        System.out.println("Sorted array :" + Arrays.toString(unsorted));
    }
}
 
class QuickSortName {
 
    private String input[];
    private int length;
 
    public void sort(String[] names) {
 
        if (names == null || names.length == 0) {
            return;
        }
 
        this.input = names;
        length = names.length;
        quickSort(0, length - 1);
    }
 
    /*
     * This method implements in-place quicksort algorithm recursively.
     */
    private void quickSort(int low, int high) {
        int i = low;
        int j = high;
 
        // pivot is middle index
        String pivot = input[low + (high - low) / 2];
 
        // Divide into two arrays
        while (i <= j) {
            /**
             * As shown in above image, In each iteration, we will identify a
             * number from left side which is greater then the pivot value, and
             * a number from right side which is less then the pivot value. Once
             * search is complete, we can swap both numbers.
             */
            while (input[i].charAt(0) < pivot.charAt(0)) {
                i++;
            }
            while (input[j].charAt(0) > pivot.charAt(0)) {
                j--;
            }
            if (i <= j) {
                swap(i, j);
                // move index to next position on both sides
                i++;
                j--;
            }
        }
 
        // calls quickSort() method recursively
        if (low < j) {
            quickSort(low, j);
        }
 
        if (i < high) {
            quickSort(i, high);
        }
    }
 
    private void swap(int i, int j) {
        String temp = input[i];
        input[i] = input[j];
        input[j] = temp;
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Rodrigo
Val: 2.041
Plata
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

Publicado por Rodrigo (623 intervenciones) el 07/03/2020 22:34:44
Esta version de quicksort parece comparar solo la primera letra de cada palabra.
No parece mejor comparar los strings completos?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

Publicado por Costero (148 intervenciones) el 08/03/2020 01:24:28
Si, version string completos

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
import java.util.Arrays;
 
/**
 * Test class to sort array of strings using Quicksort algorithm in Java.
 *
 * @author Costero
 */
public class QuickSortDemoName {
 
    public static void main(String args[]) {
        // unsorted integer array
        String[] unsorted = {"Marcos Calderon", "Vatias Ruiz", "Daniela Goretti", "Marcos Hurtado", "Marcos Phillips"};
        System.out.println("Unsorted array :" + Arrays.toString(unsorted));
 
        QuickSortName algorithm = new QuickSortName();
 
        // sorting integer array using quicksort algorithm
        algorithm.sort(unsorted);
 
        // printing sorted array
        System.out.println("Sorted array :" + Arrays.toString(unsorted));
    }
}
 
class QuickSortName {
 
    private String input[];
    private int length;
 
    public void sort(String[] names) {
 
        if (names == null || names.length == 0) {
            return;
        }
 
        this.input = names;
        length = names.length;
        quickSort(0, length - 1);
    }
 
    /*
     * This method implements in-place quicksort algorithm recursively.
     */
    private void quickSort(int low, int high) {
        int i = low;
        int j = high;
 
        // pivot is middle index
        String pivot = input[low + (high - low) / 2];
 
        // Divide into two arrays
        while (i <= j) {
            /**
             * As shown in above image, In each iteration, we will identify a
             * number from left side which is greater then the pivot value, and
             * a number from right side which is less then the pivot value. Once
             * search is complete, we can swap both numbers.
             */
 
              // first name only ...
//            while (input[i].charAt(0) < pivot.charAt(0)) {
//                i++;
//            }
//            while (input[j].charAt(0) > pivot.charAt(0)) {
//                j--;
//            }
 
            // full string ...
            while (input[i].compareToIgnoreCase(pivot) < 0) {
                i++;
            }
            while (input[j].compareToIgnoreCase(pivot) > 0) {
                j--;
            }
 
            if (i <= j) {
                swap(i, j);
                // move index to next position on both sides
                i++;
                j--;
            }
        }
 
        // calls quickSort() method recursively
        if (low < j) {
            quickSort(low, j);
        }
 
        if (i < high) {
            quickSort(i, high);
        }
    }
 
    private int getInitial(String input) {
        String values [] = input.split(" ");
        return (int) values[0].charAt(0) + (int) values[1].charAt(0);
    }
 
    private void swap(int i, int j) {
        String temp = input[i];
        input[i] = input[j];
        input[j] = temp;
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 129
Ha disminuido 1 puesto en Java (en relación al último mes)
Gráfica de Java

¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

Publicado por Juan Manuel (53 intervenciones) el 07/03/2020 22:35:29
creo que tambien se puede con el metodo compare

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MetodoSort {
 
	public static void main(String[] args) {
		String[] nombre = { "manuel", "celina", "paola", "jordan", "anastacio", "ana", "arturo" };
 
		for (int i = 0; i < nombre.length - 1; i++) {
			for (int h = 0; h < nombre.length - 1; h++) {
				if ((nombre[h].compareTo(nombre[h + 1])) < 0) {//si el metodo compare da mayor a 0 es ascendente y si da menor es desendente
					String aux = nombre[h];
					nombre[h] = nombre[h + 1];
					nombre[h + 1] = aux;
				}
			}
		}
 
		for (int i = 0; i < nombre.length; i++) {
			System.out.println(nombre[i]);
		}
 
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 47
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

¿Como puedo usar Quicksort para ordenar nombres alfabéticamente?

Publicado por Pedro (25 intervenciones) el 09/03/2020 21:21:51
Muchas gracias!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar