Advertisement
TSorbera

Untitled

Nov 12th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. public static void Main (string[] args)
  2. {
  3.        
  4.         int i;
  5.         int[] arrOne = {503,087,512,061,908,170,897,275,653,426,154,509,612,677,765,703};
  6.         int[] arrTwo = {908,897,765,703,677,653,612,512,509,503,426,275,170,154,087,061};
  7.         int[] arrThree = {300};
  8.         int[] arrFour = {0,1,0,0,1,1,1,0,1,0};
  9.         int n = 0;
  10.         string c = String.Empty;
  11.        
  12.         // the application runs until the user agrees to quit at the end of a sorting run
  13.         do
  14.         {
  15.                 // displays the 4 arrrays
  16.                 Console.WriteLine ("Assignment: Given are 4 different arrays; You have the choice which one you want to sort.");
  17.                 Console.WriteLine ("\n1. Knuths entry sequence:\n   503,087,512,061,908,170,897,275,653,426,154,509,612,677,765,703");
  18.                 Console.WriteLine ("\n2. Knuths entry sequence descended:\n   908,897,765,703,677,653,612,512,509,503,426,275,170,154,087,061");
  19.                 Console.WriteLine ("\n3. Just one number:\n   300");
  20.                 Console.WriteLine ("\n4. A set of 0 and 1:\n   0,1,0,0,1,1,1,0,1,0");
  21.                 Console.WriteLine ("\n Which array do you want to sort?");
  22.                 n = Convert.ToInt32(Console.ReadLine());
  23.                 //Console.Clear();
  24.                 var comp = new CountingComparer<int>();
  25.                
  26.                 switch (n)
  27.                 {
  28.                 case 1:
  29.                         for (i = arrOne.Length; i > 1; i--)
  30.                         {
  31.                                 HeapSort(arrOne, i - 1, comp);
  32.                         }
  33.                         DisplayArray(arrOne);
  34.                         break;
  35.                 case 2:
  36.                         for (i = arrTwo.Length; i > 1; i--)
  37.                         {
  38.                                 HeapSort(arrTwo, i - 1, comp);
  39.                         }
  40.                         DisplayArray(arrTwo);
  41.                         break;
  42.                 case 3:
  43.                         for (i = arrThree.Length; i > 1; i--)
  44.                         {
  45.                                 HeapSort(arrThree, i - 1, comp);
  46.                         }
  47.                         DisplayArray(arrThree);
  48.                         break;
  49.                 case 4:
  50.                         for (i = arrFour.Length; i > 1; i--)
  51.                         {
  52.                                 HeapSort(arrFour, i - 1, comp);
  53.                         }
  54.                         DisplayArray(arrFour);
  55.                         break;
  56.                 default:
  57.                         Console.WriteLine ("\n Array not exist!");
  58.                         break;
  59.                 }
  60.                        
  61.                 Console.WriteLine("\nThe case needed " + comp.Count + " comparisons!");
  62.                 Console.WriteLine("\nDo you want to quit the application? (y/n) ");
  63.                 c = Console.ReadLine();
  64.                 //Console.Clear();
  65.                        
  66.         }
  67.         while (c != "y");
  68.        
  69.        
  70. }
  71.  
  72. // displays the array
  73. public static void DisplayArray(int[] arr)
  74. {
  75.         Console.WriteLine("\nSorted Array!\n");
  76.        
  77.         for (int i = 0; i < arr.Length; i++)
  78.                 Console.WriteLine ("   " + arr[i]);
  79. }
  80.  
  81. // sorts the array with the heapsort algorithm
  82. // first the root node gets selected and then the right and left child gets selected
  83. // if value of child is greater than its parent node then they get swapped
  84. // after creating heap condition the algo removes the largest element and places it in the new array of sorted elements
  85. public static void HeapSort<T>(T[] array, int arr_ubound, IComparer<T> comp)
  86. {
  87.         int i, j;
  88.         int lChild, rChild, pNode, root;
  89.         T temp;
  90.        
  91.         root = (arr_ubound - 1) / 2;
  92.        
  93.         // for debugging
  94.         //Console.WriteLine ("root " + root);
  95.         //Console.WriteLine ("arr_ubound " + arr_ubound);
  96.        
  97.         for (j = root; j >= 0; j--)
  98.         {
  99.                 for (i = root; i >= 0; i--)
  100.                 {
  101.                         lChild = (2*i)+1;
  102.                         rChild = (2*i)+2;
  103.                        
  104.                         if ((lChild <= arr_ubound) && (rChild <= arr_ubound))
  105.                         {
  106.                                 if (comp.Compare(array[rChild], array[lChild]) >= 0)
  107.                                         pNode = rChild;
  108.                                 else
  109.                                         pNode = lChild;
  110.                         }
  111.                         else
  112.                         {
  113.                                 if (rChild > arr_ubound)
  114.                                         pNode = lChild;
  115.                                 else
  116.                                         pNode = rChild;
  117.                         }
  118.                        
  119.                         if (comp.Compare(array[i], array[pNode]) < 0)
  120.                         {
  121.                                 temp = array[i];
  122.                                 array[i] = array[pNode];
  123.                                 array[pNode] = temp;
  124.                         }
  125.                 }
  126.         }
  127.        
  128.         temp = array[0];
  129.         array[0] = array[arr_ubound];
  130.         array[arr_ubound] = temp;
  131.         return;
  132. }
  133. public class CountingComparer<T> : Comparer<T>
  134. {
  135.     public int Count { get; private set; }
  136.     IComparer<T> defaultComparer = Comparer<T>.Default;
  137.     public override int Compare(T left, T right)
  138.     {
  139.         this.Count++;
  140.         return defaultComparer.Compare(left, right);
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement