Advertisement
simeon3000

Array Histogram

Sep 21st, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1.         static void Main()
  2.         {
  3.             List<string> inList = Console.ReadLine()
  4.                                .Split(' ').ToList();
  5.  
  6.             List<string> wordList = new List<string>();
  7.             List<int> countList = new List<int>();
  8.  
  9.             for (int i = 0; i < inList.Count; i++)
  10.             {
  11.                 if (!wordList.Contains(inList[i]))
  12.                 {
  13.                     wordList.Add(inList[i]);
  14.                     countList.Add(1);
  15.                 }
  16.                 else
  17.                 {
  18.                     for (int j = 0; j < wordList.Count; j++)
  19.                     {
  20.                         if (wordList[j].Equals(inList[i]))
  21.                         {
  22.                             countList[j]++;
  23.                         }
  24.                     }
  25.                 }
  26.             }
  27.  
  28.             BubleSort(wordList, countList);
  29.  
  30.             int sum = 0;
  31.             for (int i = 0; i < countList.Count; i++)
  32.             {
  33.                 sum += countList[i];
  34.             }
  35.  
  36.             for (int i = 0; i < wordList.Count; i++)
  37.             {              
  38.                 double percentage = countList[i] / (double)sum * 100;
  39.                 Console.WriteLine($"{wordList[i]} -> {countList[i]} times ({percentage:f2}%)");
  40.             }
  41.         }
  42.  
  43.         private static void BubleSort(List<string> list1, List<int> list2)
  44.         {
  45.             bool isSorted = true;
  46.             do
  47.             {
  48.                 isSorted = true;
  49.                 for (int i = 0; i < list1.Count - 1; i++)
  50.                 {
  51.                     if (list2[i] < list2[i + 1])
  52.                     {
  53.                         string temp1 = list1[i];
  54.                         list1[i] = list1[i + 1];
  55.                         list1[i + 1] = temp1;
  56.  
  57.                         int temp2 = list2[i];
  58.                         list2[i] = list2[i + 1];
  59.                         list2[i + 1] = temp2;
  60.  
  61.                         isSorted = false;
  62.                     }
  63.                 }
  64.             } while (!isSorted);
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement