Sort a list (Java) Ultimamente a causa degli impegni tra cui la prova in itinere Programming 1, which today I got the results! I have not posted anything on my years in java! (Although he has made plenty of them).
studying the other day on another computer I tried to implement some algorithms that I know a few words.
then I wrote a program that took a list of strings (an array) where the order of the strings is random, it is arranged in a lexicographic principle
. (Click to learn more about Wikipedia.)
Given the widespread use of the methods during the preparation for the test in progress by now I have a habit of splitting in these "sealed rooms" my program ...
Checkout to know? parola1.compareTo (word2) : Suppose word1 and word2 are two strings, this method returns an integer that indicates which string comes first in lexicographic order,
If a negative number, a word must be placed before the word 2 if so, vice versa.
If you get 0, the two words are exactly the same.
description of the methods extent on the methods "fundamentals" ... The find method is one of those sealed rooms that I mentioned above (for the method you want a piece of code "tight", that returns a result parameter data, the programmer then you should take to verify exactly what it does il metodo, gli basta sapere cosa vuole sapere(parametri in ingresso) e cosa da(parametri in uscita))
questo metodo data un'arrai di stringhe ed una parola, restituisce l'indice in cui la parola andrebbe inserita, ad esempio se l'array contiene le parole(le parentesi indicano gli indici): barca(0),casa(1),mare(2) e noi inseriamo la parola "dattero" ci restituisce il numero 2.
Il metodo inserisci invece preso un indice, una stringa ed un array di stringhe, da in output un array di stringhe dove nell'indice dato è stata inserita la parola e le parole che stavano a partire da quel indice, vengono spostate di un posto (di volta in volta l'ultima parola viene persa)
Adesso possiamo passare Central to the method that is the sort method, this method takes an array of strings and returns a sorted using the two methods used above.
Here is the code ... You should understand something else:
-
public class Ordinamentok
-
{
-
public static void main
( String [ ] args )
-
{
-
String [ ] messy
= & # 123; "Smith" ,
"Iemolo" ,
"Cavallaro" ,
"Christmas"
-
, "Carrubba" , "Dima" , "Lanza" , "no name "} ;
-
String [ ] ordered
order
= & # 40; disordered
) ;
-
for ( int i = 0 ; i < ordinato. length ; i ++ )
-
{
-
System .
out.
println ( ordered
[ the
] ) ;
-
}
-
}
-
public static String [ ] order
( String [ ] A
)
-
{
-
String [ ] B
= new String [ A.
length ] ;
-
for ( int j = 0 ; j \u0026lt; B. length ; j + + )
-
; {
-
B [ j] = "~ ~" ;
-
}
-
for ( int i = 0; the \u0026lt; A. length ; i + + )
-
{
-
; int index = find ( B, A [ the ] ) ;
-
B = post & # 40; A [ the ] , B, index ) ;
-
}
-
return B;
-
}
-
public static String [ ] post
& # 40; String word, String
[ ] A, int
c)
-
{
-
String [ ] out
= new String [ A.
length ] ;
-
if ( c > 0 )
-
{
-
for ( int i = 0 ; i < c ; i ++ )
-
{
-
out [ i ] = A [ the ] ;
-
}
-
}
-
out [ c] = word ;
-
; for ( int j = c + 1 ; j < out. length ; j ++ )
-
{
-
out [ j ] = A [ j - 1 ] ;
-
}
-
return out ;
-
}
-
-
{
-
int sup = lista. length ;
-
int inf = 0 ;
-
while ( ( ( sup - ( inf ) ) / 2 ) >= 1 )
-
{
-
int a = ( ( sup - ( inf ) ) / 2 ) + inf ;
-
if ( ( parola. compareTo ( lista [ a ] ) ) < 0 )
-
{
-
sup = a ;
-
}
-
else
-
{
-
= inf to ;
-
}
-
}
-
if ( word. compareTo (list [sup ] ) \u0026lt; 0)
-
return inf;
-
return sup ;
-
}
-
}
for any questions at your disposal! PS
the test went quite well! I got 30,-p