Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the name of picture that you want to be sorted (with extension): ");
            string imagePath = Console.ReadLine();

            Directory.SetCurrentDirectory(@"..\..\..\Pictures");
            // check if the specifed file exists
            if (!File.Exists(imagePath))
            {
                Console.WriteLine("The specifed file \"{0}\" could not be found!", imagePath);
                return;
            }
            // converting from jpg to bmp file
            var    name  = Path.GetFileNameWithoutExtension(imagePath);
            Bitmap image = new Bitmap(imagePath);

            image.Save(name + ".bmp", ImageFormat.Bmp);
            // actual sorting
            PixelArray array = new PixelArray(name + ".bmp");

            HeapSortArray.Sort(array);
            // outputing picture
            array.OutputPicture(name + "_Sorted.bmp");
            Console.WriteLine("File sorted");
            File.Delete(name + ".bmp");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public static void Sort(PixelArray bytes)
        {
            for (int i = bytes.PixelCount / 2 - 1; i >= 0; i--)
            {
                Heapify(bytes, bytes.PixelCount, i);
            }

            for (int i = bytes.PixelCount - 1; i > 0; i--)
            {
                bytes.SwapAllBytes(0, i);
                Heapify(bytes, i, 0);
            }
        }
Ejemplo n.º 3
0
        private static void Heapify(PixelArray bytes, int size, int root)
        {
            int maxIndex   = root;
            int leftIndex  = 2 * root + 1;
            int rightIndex = 2 * root + 2;

            if (leftIndex < size && bytes.GetPixel_Red(leftIndex) > bytes.GetPixel_Red(maxIndex))
            {
                maxIndex = leftIndex;
            }

            if (rightIndex < size && bytes.GetPixel_Red(rightIndex) > bytes.GetPixel_Red(maxIndex))
            {
                maxIndex = rightIndex;
            }

            if (maxIndex != root)
            {
                bytes.SwapAllBytes(root, maxIndex);
                Heapify(bytes, size, maxIndex);
            }
        }