Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OrdinaStringhe;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.SortedMap;
- import java.util.TreeMap;
- public class ordinaCognomi {
- private static Map<String,Integer> m = new TreeMap<String,Integer>();
- /**
- * //bubble normale
- for(int i = 1; i < a.length-1; i++)
- for(int j = 0; j < a.length-i; j++)
- if(a[j].compareTo(a[j+1]) > 0) {
- String k = a[j];
- a[j] = a[j+1];
- a[j+1] = k;
- }
- */
- /**
- * Bubble migliorato
- * Ordina i cognomi in ordine crescente
- * @param a
- */
- public static void ordinaCognomiCrescente(String[] a) {
- boolean finito = false;
- boolean fattoScambio;
- int i = 0; // zero scambi
- while(!finito) {
- i += 1;
- fattoScambio = false;
- for(int j = 0; j < a.length-1; j++)
- if(a[j].compareTo(a[j+1]) > 0) {
- String k = a[j];
- a[j] = a[j+1];
- a[j+1] = k;
- fattoScambio = true;
- }
- if((!fattoScambio) || (i == a.length-1)) {
- finito = true;
- }
- }
- trovaFreq(a);
- }
- /**
- * trova le occorrenze di ogni cognome
- * @param s
- */
- public static void trovaFreq(String[] s) {
- Integer freq = 0;
- for (int i=0; i < s.length; i++) {
- freq = m.get(s[i]);
- if (freq != null)
- m.put(s[i], freq+1);
- else
- m.put(s[i],1);
- }
- }
- public static void main(String[] args) {
- String[] s = {"lana","binchi","rossi","rossi","dura","dura","rossi","rossi"};
- ordinaCognomiCrescente(s);
- for(String h:s)
- System.out.print(h + " ");
- System.out.println("\nNum Cognomi frequenti : " + m);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement