Interesting Thing About Sorting in Java

Discussion on Java at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
Post Reply
Raudel_UH
Posts: 4
Joined: 7 years ago
Gender: None specified

Interesting Thing About Sorting in Java

Post by Raudel_UH » 6 years ago

In Java when you use Arrays.sort() you are sorting with Quick Sort and when you use
Collections.sor() you are using Merge Sort so take out your own conclusions.



User avatar
jelara
Posts: 37
Joined: 7 years ago
Location: Guadalajara, Mexico
Gender: Male
Mexico

Re: Interesting Thing About Sorting in Java

Post by jelara » 5 years ago

Thanks for the tip! I'm sure we'll all take this into account when solving our next exercise.

GoogleBot
Posts: 2
Joined: 5 years ago
Gender: None specified

Re: Interesting Thing About Sorting in Java

Post by GoogleBot » 5 years ago

Raudel_UH wrote:In Java when you use Arrays.sort() you are sorting with Quick Sort and when you use
Collections.sor() you are using Merge Sort so take out your own conclusions.
Arrays.sort(pa); Sorts the elements of the array of a primitive type into ascending order using their natural ordering.

Arrays.sort(pa, from, to); Sorts the elements pa[from]...pa[to-1] of a primitive type. into ascending order.

Arrays.sort(oa); Sorts the elements of the array of an object type into ascending order, using the order defined by Comparable interface, which defines the compareTo method. Note that many Java classes such as String (but not
StringBuffer), Double, BigInteger, etc implement Comparable.

Arrays.sort(oa, from, to); Sorts the elements of the array, in the range from...to of an object type into ascending order.

Arrays.sort(oa, comp); Sorts the elements of the array of an object type into ascending order, using the Comparator comp.

Arrays.sort(oa, from, to, comp); Sorts the elements of the array, in the range from...to of an object type into ascending order using the Comparator comp.

GoogleBot
Posts: 2
Joined: 5 years ago
Gender: None specified

Re: Interesting Thing About Sorting in Java

Post by GoogleBot » 5 years ago

Hola a todos, tengo el siguiente codigo y no logro entender el resultado luego de realizar el sort, como hace el sort() para ordenar?

El resultado del siguiente codigo es:

nickel button key lint OK, IMPRIMER TAL CUAL ESTA EL ARRAY.
nickel lint key button ESTO NO LO ENTIENDO, COMO SE ENCARGA DE ORDENAR ESTO??

Código:
package prueba.sort;


import java.util.Arrays;
import java.util.Comparator;

public class PRUEBASORT {

public static void main(String[] args) {

String [] sa = {"nickel","button","key","lint"};

Sorter s = new Sorter();

for(String s2 : sa) {
System.out.print(s2 + " ");
}

System.out.println("");

Arrays.sort(sa,s);

for(String s2 : sa) {
System.out.print(s2 + " ");
}
}

static class Sorter implements Comparator<String> {

@Override
public int compare(String a, String b) {

return b.compareTo(a);
}

}
}

kangoonie
Posts: 4
Joined: 5 years ago
Gender: None specified

Re: Interesting Thing About Sorting in Java

Post by kangoonie » 5 years ago

Hola a todos, tengo el siguiente codigo y no logro entender el resultado luego de realizar el sort, como hace el sort() para ordenar?
El codigo ordena segun la implementacion de la interface Comparator<String>, que esta implementada en la clase Sorter, y llama al metodo

Code: Select all

public int compare(String a, String b) {
return b.compareTo(a);
}
para comparar dos String. Dado que en el metodo se llama a

Code: Select all

return b.compareTo(a);
se invierte el ordenamiento. Para ordenar ascendentemente, utiliza

Code: Select all

return a.compareTo(b);

Post Reply

Return to “Java”