如何将选择排序方法应用于按字母顺序排序?

huangapple go评论65阅读模式
英文:

How to apply Selection method to sort in alphabetical order?

问题

Estudiante[] aux = new Estudiante[1];
int contador = 0;
int contador2 = 1;
for (int i = 1; i < arregloEstudiante.length - 1; i++) {
    String minimo = arregloEstudiante[contador].getCarnet();
    int menor = i;
    for (int j = i + 1; j < arregloEstudiante.length; j++) {
        String maximo = arregloEstudiante[contador2].getCarnet();
        if (minimo.compareTo(maximo) < 0) {
            menor = j;
        }
        if (menor == j) {
            aux[0] = arregloEstudiante[i];
            arregloEstudiante[i] = arregloEstudiante[menor];
            arregloEstudiante[menor] = aux[0];
        }
    }
    contador++;
    contador2++;
}
return arregloEstudiante;

// Array to be sorted:
// C54411
// B92542
// A95720
// A22523
// B4562
// B32567
// B42667
// C72588
// C42214
// C34767

// Class definition:
class Estudiante {
    private String carnet;
    private String nombre;
    private String apellidos;
    private int notaAdmision;

    public Estudiante() {
    }

    public Estudiante(String carnet, String nombre, String apellidos, int notaAdmision) {
        this.carnet = carnet;
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.notaAdmision = notaAdmision;
    }

    public String getCarnet() {
        return this.carnet;
    }

    public String getNombre() {
        return this.nombre;
    }

    public String getApellidos() {
        return this.apellidos;
    }

    public int getNotaAdmision() {
        return this.notaAdmision;
    }

    public void setCarnet(String carnet) {
        this.carnet = carnet;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public void setNotaAdmision(int notaAdmision) {
        this.notaAdmision = notaAdmision;
    }
}

The provided code appears to be a sorting algorithm using the selection sort method to sort an array of Estudiante objects based on their "carnet" attribute in ascending order. The Estudiante class defines the structure of the objects being sorted.

英文:
Estudiante[] aux = new Estudiante[1];
int contador = 0;
int contador2 = 1;
for (int i = 1; i &lt; arregloEstudiante.length - 1; i++) {
String minimo = arregloEstudiante[contador].getCarnet();
int menor = i;
for (int j = i + 1; j &lt; arregloEstudiante.length; j++) {
String maximo = arregloEstudiante[contador2].getCarnet();
if (minimo.compareTo(maximo) &lt; 0) {
menor = j;
}
if (menor == j) {
aux[0] = arregloEstudiante[i];
arregloEstudiante[i] = arregloEstudiante[menor];
arregloEstudiante[menor] = aux[0];
}
}
contador++;
contador2++;
}
return arregloEstudiante;

This is the my closest result.

C54411
B92542
A95720
A22523
B4562
B32567
B42667
C72588
C42214
C34767

We have to use Selection method. using this array

		arregloEstudiante[0] = new Estudiante(&quot;C54411&quot;, &quot;Maria&quot;, &quot;Mora Mora&quot;, 700);
arregloEstudiante[1] = new Estudiante(&quot;B92542&quot;, &quot;Jose&quot;, &quot;Solano Solano&quot;, 444);
arregloEstudiante[2] = new Estudiante(&quot;C42214&quot;, &quot;Alonso&quot;, &quot;Solano Mora&quot;, 800);
arregloEstudiante[3] = new Estudiante(&quot;A95720&quot;, &quot;Miguel&quot;, &quot;Mora Solano&quot;, 550);
arregloEstudiante[4] = new Estudiante(&quot;B32567&quot;, &quot;Andrea&quot;, &quot;Jimenez Ure&#241;a&quot;, 625);
arregloEstudiante[5] = new Estudiante(&quot;C34767&quot;, &quot;Fabian&quot;, &quot;Sanchez Alvarado&quot;, 740);
arregloEstudiante[6] = new Estudiante(&quot;C72588&quot;, &quot;Martin&quot;, &quot;Moya Ure&#241;a&quot;, 592);
arregloEstudiante[7] = new Estudiante(&quot;B42667&quot;, &quot;Fabiana&quot;, &quot;Sanchez Alvarado&quot;, 689);
arregloEstudiante[8] = new Estudiante(&quot;A22523&quot;, &quot;Mariano&quot;, &quot;Mora Mora&quot;, 750);
arregloEstudiante[9] = new Estudiante(&quot;B4562&quot;, &quot;Alonso&quot;, &quot;Solano Morales&quot;, 497);

only using the first attribute, I save them in temp, "minimo" and "maximo"`
This is the my closest result.

C54411
B92542
A95720
A22523
B4562
B32567
B42667
C72588
C42214
C34767

I have to use Selection order methods. Using this array, I have to make an alphabetical order, with the first attribute, I save them in temp "minimo" and "maximo".

I use this class.

public class Estudiante {
private String carnet;
private String nombre;
private String apellidos;
private int notaAdmision;
public Estudiante() {
}
public Estudiante(String carnet, String nombre, String apellidos, int notaAdmision) {
this.carnet = carnet;
this.nombre = nombre;
this.apellidos = apellidos;
this.notaAdmision = notaAdmision;
}
public String getCarnet() {
return this.carnet;
}
public String getNombre() {
return this.nombre;
}
public String getApellidos() {
return this.apellidos;
}
public int getNotaAdmision() {
return this.notaAdmision;
}
public void setCarnet(String carnet) {
this.carnet = carnet;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public void setNotaAdmision(int notaAdmision) {
this.notaAdmision = notaAdmision;
}

This is my class I think everything is ok.
I have to use the selection order method.
If someone finds a solution please tell me.
Compiles with JAVA 1.8

答案1

得分: 0

你正在使用选择排序来根据carnet属性对Estudiante数组进行排序。

我从这个网页上复制了代码,并进行了修改以适应您的需求。

import java.util.Arrays;

public class Estudiante {
    // ...(与原始代码相同的属性和方法)

    public static void sortAscending(final Estudiante[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minElementIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minElementIndex].getCarnet().compareTo(arr[j].getCarnet()) > 0) {
                    minElementIndex = j;
                }
            }
            if (minElementIndex != i) {
                Estudiante temp = arr[i];
                arr[i] = arr[minElementIndex];
                arr[minElementIndex] = temp;
            }
        }
    }

    public static void main(String[] args) {
        // ...(与原始代码相同的main方法)
    }
}
  • 我添加的唯一方法是sortAscending()
  • 代码的最后几行只是简单地打印出已排序的数组。它们不影响实际的排序操作。您可以使用任何方法来打印已排序的数组,如果需要打印的话。

下面是我在运行上述代码时得到的输出结果。

A22523
A95720
B32567
B42667
B4562
B92542
C34767
C42214
C54411
C72588
英文:

You are using selection sort to sort the array of Estudiante according to the carnet attribute.

I copied the code from this Web page and modified it to suit your needs.

import java.util.Arrays;

public class Estudiante {
    private String carnet;
    private String nombre;
    private String apellidos;
    private int notaAdmision;

    public Estudiante(String carnet, String nombre, String apellidos, int notaAdmision) {
        this.carnet = carnet;
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.notaAdmision = notaAdmision;
    }

    public String getCarnet() {
        return this.carnet;
    }

    public String getNombre() {
        return this.nombre;
    }

    public String getApellidos() {
        return this.apellidos;
    }

    public int getNotaAdmision() {
        return this.notaAdmision;
    }

    public void setCarnet(String carnet) {
        this.carnet = carnet;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public void setNotaAdmision(int notaAdmision) {
        this.notaAdmision = notaAdmision;
    }

    public static void sortAscending(final Estudiante[] arr) {
        for (int i = 0; i &lt; arr.length - 1; i++) {
            int minElementIndex = i;
            for (int j = i + 1; j &lt; arr.length; j++) {
                if (arr[minElementIndex].getCarnet().compareTo(arr[j].getCarnet()) &gt; 0) {
                    minElementIndex = j;
                }
            }
            if (minElementIndex != i) {
                Estudiante temp = arr[i];
                arr[i] = arr[minElementIndex];
                arr[minElementIndex] = temp;
            }
        }
    }

    public static void main(String[] args) {
        Estudiante[] arregloEstudiante = new Estudiante[10];
        arregloEstudiante[0] = new Estudiante(&quot;C54411&quot;, &quot;Maria&quot;, &quot;Mora Mora&quot;, 700);
        arregloEstudiante[1] = new Estudiante(&quot;B92542&quot;, &quot;Jose&quot;, &quot;Solano Solano&quot;, 444);
        arregloEstudiante[2] = new Estudiante(&quot;C42214&quot;, &quot;Alonso&quot;, &quot;Solano Mora&quot;, 800);
        arregloEstudiante[3] = new Estudiante(&quot;A95720&quot;, &quot;Miguel&quot;, &quot;Mora Solano&quot;, 550);
        arregloEstudiante[4] = new Estudiante(&quot;B32567&quot;, &quot;Andrea&quot;, &quot;Jimenez Ure&#241;a&quot;, 625);
        arregloEstudiante[5] = new Estudiante(&quot;C34767&quot;, &quot;Fabian&quot;, &quot;Sanchez Alvarado&quot;, 740);
        arregloEstudiante[6] = new Estudiante(&quot;C72588&quot;, &quot;Martin&quot;, &quot;Moya Ure&#241;a&quot;, 592);
        arregloEstudiante[7] = new Estudiante(&quot;B42667&quot;, &quot;Fabiana&quot;, &quot;Sanchez Alvarado&quot;, 689);
        arregloEstudiante[8] = new Estudiante(&quot;A22523&quot;, &quot;Mariano&quot;, &quot;Mora Mora&quot;, 750);
        arregloEstudiante[9] = new Estudiante(&quot;B4562&quot;, &quot;Alonso&quot;, &quot;Solano Morales&quot;, 497);
        sortAscending(arregloEstudiante);
        Arrays.stream(arregloEstudiante)
              .forEach(e -&gt; System.out.println(e.getCarnet()));
    }
}
  • The only method I added was sortAscending().
  • The last lines of the code simply print out the sorted array. They do not affect the actual sort operation. You can use any method you like to print out the sorted array &ndash; if you need to print it out at all.

Below is the output I get when running the above code.

A22523
A95720
B32567
B42667
B4562
B92542
C34767
C42214
C54411
C72588

huangapple
  • 本文由 发表于 2020年9月11日 23:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63850410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定