Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1.  
  2. public class ordinaCognomi {
  3.  
  4. private static Map<String,Integer> m = new TreeMap<String,Integer>();
  5.  
  6. /**
  7. * //bubble normale
  8. for(int i = 1; i < a.length-1; i++)
  9. for(int j = 0; j < a.length-i; j++)
  10.  
  11. if(a[j].compareTo(a[j+1]) > 0) {
  12.  
  13. String k = a[j];
  14. a[j] = a[j+1];
  15. a[j+1] = k;
  16. }
  17.  
  18. */
  19.  
  20.  
  21. /**
  22. * Ordina i cognomi in ordine crescente
  23. * @param a
  24. */
  25. public static void ordinaCognomiCrescente(String[] a) {
  26.  
  27. boolean finito = false;
  28. boolean fattoScambio;
  29. int i = 0; // zero scambi
  30.  
  31. while(!finito) {
  32. i += 1;
  33. fattoScambio = false;
  34. for(int j = 0; j < a.length-1; j++)
  35. if(a[j].compareTo(a[j+1]) > 0) {
  36.  
  37. String k = a[j];
  38. a[j] = a[j+1];
  39. a[j+1] = k;
  40. fattoScambio = true;
  41. }
  42. if((!fattoScambio) || (i == a.length-1)) {
  43. finito = true;
  44. }
  45. }
  46. trovaFreq(a);
  47. }
  48.  
  49.  
  50. /**
  51. * trova le occorrenze di ogni cognome
  52. * @param s
  53. */
  54. public static void trovaFreq(String[] s) {
  55.  
  56. Integer freq = 0;
  57.  
  58. for (int i=0; i < s.length; i++) {
  59.  
  60. freq = m.get(s[i]);
  61.  
  62. if (freq != null)
  63. m.put(s[i], freq+1);
  64. else
  65. m.put(s[i],1);
  66. }
  67.  
  68.  
  69. }
  70.  
  71. public static void main(String[] args) {
  72.  
  73. String[] s = {"lana","binchi","rossi","rossi","dura","dura","rossi","rossi"};
  74. ordinaCognomiCrescente(s);
  75.  
  76. for(String h:s)
  77. System.out.print(h + " ");
  78.  
  79. System.out.println("\n\nNum Cognomi frequenti : " + m);
  80. }
  81.  
  82.  
  83. ============================= output ==================================
  84.  
  85. binchi dura dura lana rossi rossi rossi rossi
  86.  
  87. Num Cognomi frequenti : {binchi=1, dura=2, lana=1, rossi=4}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement