Advertisement
Guest User

Untitled

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