static void heapSort(MyFileArray arr, int n) { for (int i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); } for (int i = n - 1; i >= 0; i--) { heapify(arr, i, 0); } }
public static void Test_File_Array_List_heap(int seed, ref Stopwatch watch) { int n = 30000; string filename; filename = @"mydataarray.dat"; MyFileArray heapFileArray = new MyFileArray(filename, n, seed); using (heapFileArray.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite)) { Console.WriteLine("\n HEAP FILE ARRAY \n"); // heapFileArray.Print(n); var time = System.Diagnostics.Stopwatch.StartNew(); heapSort(heapFileArray, n); time.Stop(); Console.WriteLine("Uztruko: {0}ms", time.Elapsed.TotalMilliseconds); // heapFileArray.Print(n); } }
static void heapify(MyFileArray arr, int n, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < n && arr[left].CompareTo(arr[largest]) > 0) { largest = left; } if (right < n && arr[right].CompareTo(arr[largest]) > 0) { largest = right; } if (largest != i) { arr.Swap(i, arr[i], largest, arr[largest]); //var swap = arr[i]; //arr[i] = arr[largest]; //arr[largest] = swap; heapify(arr, n, largest); } }