Candeias Posted December 1, 2009 at 03:43 PM Report Share #298238 Posted December 1, 2009 at 03:43 PM Boa tarde, precisso de ordenar um array com utilizando o compareTo, mas está a falta a logica penso eu..:S Estava a pensar fazer um ciclo FOR para correr as posicoes da arrays, e dentro deste fazia outro FOR para verificar onde esta irá ficar... Confuso..lol Exemplo: String vetor[] = new String[20] vector[0]="a"; vector[1]="c"; vector[2]="z"; vector[3]="d"; vector[4]="r"; vector[5]="b"; vector[6]="i"; For (0 ate 6) For(0 ate as posicoes ja lidas) Valida se a letra é maior menos ou igual... nao sei como fazer esta parte :S Aguem pode dar uma dica?? Obrigado Link to comment Share on other sites More sharing options...
bruno1234 Posted December 1, 2009 at 03:49 PM Report Share #298240 Posted December 1, 2009 at 03:49 PM Podes usar o método compareTo para comparar as strings. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
magician Posted December 1, 2009 at 04:43 PM Report Share #298248 Posted December 1, 2009 at 04:43 PM http://pt.wikipedia.org/wiki/Bubble_sort#Java ao invés de um array de int usas o teu de String e usas o valor retornado pelo CompareTo para fazer a comparação. I haven’t lost my mind; it’s backed up on DVD somewhere! Link to comment Share on other sites More sharing options...
Candeias Posted December 1, 2009 at 06:58 PM Author Report Share #298271 Posted December 1, 2009 at 06:58 PM Continua a faltar a logica... eu tenho: for (int i=0; cont <index; i++) { if (vetor[i].compareTo(vetor[i +1 ]) < 0) { //letra i é MENOR que i+1 System.out.println("a letra " +vetor[i].toString()+"é menor que "+ vetor[i+1]); } else if (vetor[i].compareTo(vetor[i + 1]) == 0) { //iguais System.out.println("0"); } else if (vetor[i].compareTo(vetor[i +1]) > 0) { //letra i é MAIOR que i+1 System.out.println("a letra " +vetor[i].toString()+"é maior que "+ vetor[i+1]); } } Agora em vez dos System.Out, o que devo fazer? Falta alguma coisa no meu FOR? Link to comment Share on other sites More sharing options...
magician Posted December 1, 2009 at 07:26 PM Report Share #298277 Posted December 1, 2009 at 07:26 PM Por mero acaso olhas-te para o link que te dei ? Aquilo é o algoritmo não precisas de nada mais do que aquilo. I haven’t lost my mind; it’s backed up on DVD somewhere! Link to comment Share on other sites More sharing options...
Candeias Posted December 1, 2009 at 07:51 PM Author Report Share #298290 Posted December 1, 2009 at 07:51 PM public static void main(String[] args) { String vetor[] = new String[100]; vetor[0] = "z"; vetor[1] = "a"; vetor[2] = "g"; vetor[3] = "d"; for (int i = 0; i <vetor.length-1; i++) { for (int j = 0; j <= vetor.length-1; j++) { if ((vetor[j].compareTo(vetor[j + 1]) > 0)){ String temp = vetor[i]; vetor[i] = vetor[j]; vetor[j] = temp; } } } for (int i = 0; i < vetor.length-1; i++) { System.out.println(vetor[i]); } não estou a conseguir :S Link to comment Share on other sites More sharing options...
pedrix21 Posted December 1, 2009 at 08:17 PM Report Share #298306 Posted December 1, 2009 at 08:17 PM static <T extends Comparable<? super T>> void selectionSort( T[] a, int n ) { if ( a == null ) throw new NullPointerException(); if ( n > a.length ) throw new IndexOutOfBoundsException(); // while the size of the unsorted part is > 1 for ( int unsortedSize = n; unsortedSize > 1; unsortedSize-- ) { // find the position of the largest // element in the unsorted section int maxPos = 0; for (int pos = 1; pos < unsortedSize; pos++) if ( a[pos].compareTo(a[maxPos]) > 0 ) maxPos = pos; // postcondition: maxPos is the position // of the largest element in the unsorted // part of the array // Swap largest value with the last value // in the unsorted part T temp = a[unsortedSize - 1]; a[unsortedSize - 1] = a[maxPos]; a[maxPos] = temp; } } Desculpa não estar identado, se fores da ESTGF (parece-me que és) vai ao slide 33 do pdf Aula06.Ordenacoes_Pesquisas_V2.pdf que tem lá este código 🙂 Cumps @Pedro Lopes Link to comment Share on other sites More sharing options...
magician Posted December 1, 2009 at 08:27 PM Report Share #298311 Posted December 1, 2009 at 08:27 PM Ora ai está um exemplo com generic e tudo lol I haven’t lost my mind; it’s backed up on DVD somewhere! Link to comment Share on other sites More sharing options...
vasco16 Posted December 1, 2009 at 09:37 PM Report Share #298320 Posted December 1, 2009 at 09:37 PM static <T extends Comparable<? super T>> void selectionSort( T[] a, int n ) { if ( a == null ) throw new NullPointerException(); if ( n > a.length ) throw new IndexOutOfBoundsException(); // while the size of the unsorted part is > 1 for ( int unsortedSize = n; unsortedSize > 1; unsortedSize-- ) { // find the position of the largest // element in the unsorted section int maxPos = 0; for (int pos = 1; pos < unsortedSize; pos++) if ( a[pos].compareTo(a[maxPos]) > 0 ) maxPos = pos; // postcondition: maxPos is the position // of the largest element in the unsorted // part of the array // Swap largest value with the last value // in the unsorted part T temp = a[unsortedSize - 1]; a[unsortedSize - 1] = a[maxPos]; a[maxPos] = temp; } } Desculpa não estar identado, se fores da ESTGF (parece-me que és) vai ao slide 33 do pdf Aula06.Ordenacoes_Pesquisas_V2.pdf que tem lá este código 🙂 Cumps ca para mim ainda é da FCUL xD tambem tive um trabalho desses mas era um pouco mais complexo, era um metodo que recebia 2 arrays ordenava o primeiro em caso de empate desempatava com o segundo e se o segundo tivesse empatado fazia-se um random e depois este metodo devolvia os indices do array ordenado.. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now