Advertisement
Guest User

Untitled

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