Exemplo n.º 1
0
        // To heapify a subtree rooted with node i which is
        // an index in arr[]. n is size of heap
        void heapify(MyFileArray arr, int n, int i)
        {
            int largest = i;         // Initialize largest as root
            int l       = 2 * i + 1; // left = 2*i + 1
            int r       = 2 * i + 2; // right = 2*i + 2

            // If left child is larger than root
            if (l < n && 1 == Comparer <IntFloat> .Default.Compare(arr[l], arr[largest]))
            {
                largest         = l;
                OperationCount += 1;
            }
            // If right child is larger than largest so far
            if (r < n && 1 == Comparer <IntFloat> .Default.Compare(arr[r], arr[largest]))
            {
                largest         = r;
                OperationCount += 1;
            }
            OperationCount += 4;
            // If largest is not root
            OperationCount += 1;
            if (largest != i)
            {
                IntFloat swapp = arr[i];
                Swap(i, largest, arr);           //arr[i] = arr[largest];
                arr.WriteToFile(largest, swapp); //arr[largest] = swapp;
                OperationCount += 4;
                // Recursively heapify the affected sub-tree
                heapify(arr, n, largest);
            }
        }
Exemplo n.º 2
0
        public void sort(MyFileArray arr, int length)
        {
            int n = length;

            OperationCount += 1;
            // Build heap (rearrange array)
            for (int i = n / 2 - 1; i >= 0; i--)
            {
                heapify(arr, n, i);
                OperationCount += 2;
            }

            // One by one extract an element from heap
            for (int i = n - 1; i >= 0; i--)
            {
                // Move current root to end
                IntFloat temp = arr[0];
                Swap(i, 0, arr);          //arr[0] = arr[i];
                arr.WriteToFile(i, temp); //arr[i] = temp;

                // call max heapify on the reduced heap
                heapify(arr, i, 0);
                OperationCount += 5;
            }
        }
Exemplo n.º 3
0
        public IntFloat[] GenerateArray(int size)
        {
            Random rand = new Random();

            IntFloat[] Array = new IntFloat[size];
            for (int i = 0; i < size; i++)
            {
                Array[i] = new IntFloat(rand.Next(0, 1000), (float)rand.NextDouble());
            }
            return(Array);
        }
Exemplo n.º 4
0
 public void WriteToFile(int indexToOverRide, IntFloat info)
 {
     Byte[] data;
     fs.Seek(indexToOverRide * 12, SeekOrigin.Begin);
     data = BitConverter.GetBytes((double)info.Floater);
     fs.Write(data, 0, 8);
     data = BitConverter.GetBytes(info.Integer);
     fs.Seek(indexToOverRide * 12 + 8, SeekOrigin.Begin);
     fs.Write(data, 0, 4);
     OperationCount += 7;
 }
Exemplo n.º 5
0
 public IntFloat this[int index]
 {
     get
     {
         Byte[] data = new Byte[12];
         fs.Seek(12 * index, SeekOrigin.Begin);
         fs.Read(data, 0, 12);
         int      resultInt    = BitConverter.ToInt32(data, 8);
         double   resultDouble = BitConverter.ToDouble(data, 0);
         IntFloat result       = new IntFloat(resultInt, (float)resultDouble);
         OperationCount += 7;
         return(result);
     }
 }
Exemplo n.º 6
0
        public void Swap(int j, int i, MyFileArray arr)
        {
            IntFloat a = arr[j];
            IntFloat b = arr[i];

            Byte[] data = new Byte[24];
            fs.Seek(i * 12, SeekOrigin.Begin);
            data = BitConverter.GetBytes((double)a.Floater);
            fs.Write(data, 0, 8);
            fs.Seek((i * 12) + 8, SeekOrigin.Begin);
            data = BitConverter.GetBytes(a.Integer);
            fs.Write(data, 0, 4);
            fs.Seek(j * 12, SeekOrigin.Begin);
            data = BitConverter.GetBytes((double)b.Floater);
            fs.Write(data, 0, 8);
            fs.Seek((j * 12) + 8, SeekOrigin.Begin);
            data = BitConverter.GetBytes(b.Integer);
            fs.Write(data, 0, 4);
            OperationCount += 14;
        }
Exemplo n.º 7
0
        public void HeapSortRAM(int NumberOfElements)
        {
            int startInt, finishInt;
            int startas = 0; int finisas = 0;
            LinkedList <IntFloat> IntList         = new LinkedList <IntFloat>();
            Random    random                      = new Random();
            Stopwatch measure                     = new Stopwatch();
            HeapSortLinkedList <IntFloat> IntSort = new HeapSortLinkedList <IntFloat>();
            long IntTime;
            int  ArrayOP;
            int  ListOP;

            for (int y = 0; y < NumberOfElements; y++)
            {
                IntFloat x = new IntFloat(random.Next(0, 1000), (float)random.NextDouble());
                IntList.addItem(x);
            }
            IntFloat[] IntFloatArray = GenerateArray(NumberOfElements);

            Console.WriteLine("Ar norite pamatyti RAM atmintyje sugeneruotas aibes? Įvesti: Y/N");
            string YN    = Console.ReadLine();
            bool   yesNo = false;

            if (YN == "Y" || YN == "y")
            {
                yesNo = true;
            }
            if (yesNo)
            {
                Console.WriteLine("Nurodykite intervala, kuriuos elementus norite pamatyti:");
                Console.WriteLine("Nuo: ");
                startInt = int.Parse(Console.ReadLine());
                Console.WriteLine("Iki: ");
                finishInt = int.Parse(Console.ReadLine());
                Console.WriteLine("LIST AIBE:");
                for (int i = startInt - 1; i < finishInt - 1; i++)
                {
                    Console.WriteLine($"{(i+1)}. " +
                                      IntList.returnNodeByIndex(i).ToString());
                }
                Console.WriteLine();
                Console.WriteLine("ARRAY AIBE:");
                for (int i = startInt - 1; i < finishInt - 1; i++)
                {
                    Console.WriteLine($"{(i+1)}. " + IntFloatArray[i].ToString());
                }
            }
            Console.WriteLine("Ar norite pamatyti surikiuotas aibes? Y/N");
            string rikiuotAib = Console.ReadLine();
            bool   rikiuotYN  = false;

            if (rikiuotAib == "y" || rikiuotAib == "Y")
            {
                rikiuotYN = true;
            }
            Console.WriteLine("\nSorting in OPERATING MEMORY...");
            IntSort.init(IntList);
            measure.Reset();
            measure.Start();
            IntSort.startSort();
            measure.Stop();
            IntTime = measure.ElapsedMilliseconds;
            ListOP  = IntSort.getOpCount();
            Console.WriteLine();
            if (rikiuotYN)
            {
                Console.WriteLine($"Nurodykite intervala nuo {1} iki {NumberOfElements}: ");
                Console.WriteLine("Nuo: ");
                startas = int.Parse(Console.ReadLine());
                Console.WriteLine("Iki: ");
                finisas = int.Parse(Console.ReadLine());
                Console.WriteLine("LIST surikiuota aibe:");
                for (int i = startas - 1; i < finisas - 1; i++)
                {
                    Console.WriteLine(IntList.returnNodeByIndex(i).ToString());
                }
            }
            Console.WriteLine($"List sort: {(double)IntTime/1000} ms | Operations made: {ListOP}");
            IntList.clear();

            Console.WriteLine("-------------------------------------");

            long IntArrayTime;
            HeapSortArray <IntFloat> IntArraySort = new HeapSortArray <IntFloat>();
            Stopwatch timer = new Stopwatch();

            timer.Reset();
            timer.Start();
            IntArraySort.sort(IntFloatArray);
            timer.Stop();
            IntArrayTime = timer.ElapsedMilliseconds;
            ArrayOP      = IntArraySort.getOpCount();
            if (rikiuotYN)
            {
                Console.WriteLine("Array surikiuota aibe:");
                for (int i = startas - 1; i < finisas - 1; i++)
                {
                    Console.WriteLine(IntFloatArray[i].ToString());
                }
            }
            Console.WriteLine($"Array sort time: {(double)IntArrayTime/1000} ms | Operations made: {ArrayOP.ToString()}");
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine($"Items: {NumberOfElements} ===> List: {(double)IntTime/1000} ms | Operations made: {ListOP}  vs  Array: {(double)IntArrayTime/1000} ms | Operations made: {ArrayOP}");
            Console.WriteLine("-----------------------------------------");
        }