public static void Test_OP(int seed, int n) { ListObject data = new ListObject(); Stopwatch sw = new Stopwatch(); Random rand = new Random(seed); for (int i = 0; i < n; i++) { SortableObject temp = new SortableObject(); temp.Number = rand.Next(1, 100); for (int j = 0; j < 8; j++) { temp.Text += (char)rand.Next(65, 90); } data.Insert(temp, i); } sw.Start(); data.Print(); Heapsort_list_OP.HeapSort(data, n); data.Print(); sw.Stop(); Console.WriteLine("{1,9} took => {0}", sw.Elapsed, n); }
public static void Test_OP(int seed) { int n = 11; ListObject data = new ListObject(); Console.WriteLine("---------------------------------------------"); Stopwatch sw = new Stopwatch(); sw.Start(); Random rand = new Random(seed); for (int i = 0; i < n; i++) { SortableObject temp = new SortableObject(); temp.Number = rand.Next(1, 100); //temp.Number = i; for (int j = 0; j < 8; j++) { temp.Text += (char)rand.Next(65, 90); } data.Insert(temp, i); } sw.Stop(); Console.WriteLine("Size of {0} count was initialized in => {1}", n, sw.Elapsed); Console.WriteLine("\nHeapsort started (timer on) \n"); sw.Start(); data.Print(); Heapsort_list_OP.HeapSort(data, n); Console.WriteLine("---------------------------------------------"); data.Print(); sw.Stop(); Console.WriteLine("Test OP success "); Console.WriteLine("Size of {0} count array was sorted in => {1}", n, sw.Elapsed); Console.WriteLine("---------------------------------------------"); }
static void Heapify(ListObject arr, int n, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; Console.WriteLine("i -> {2} left-> {0} right-> {3} largest-> {1}", left, largest, i, right); arr.Print(); bool isRightLarger = arr.IsLeftBigger(right, largest); if (right < n && isRightLarger) { largest = right; Console.WriteLine("****************right is largest"); } bool isLeftLarger = arr.IsLeftBigger(left, largest); if (left < n && isLeftLarger) { largest = left; Console.WriteLine("****************left is largest"); } if (largest != i) { Console.WriteLine("****************i is NOT largest"); arr.Swap(i, largest); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++"); arr.Print(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Heapify(arr, n, largest); } else { Console.WriteLine("I AM LARGEST\n\n\n"); } }