示例#1
0
        static void Generate(int seed)
        {
            char key = GetAction(false);

            if (key == '0')
            {
                return;
            }
            Console.WriteLine("\nĮveskite dydį:");
            int size = ReadPositiveInteger();

            if (key == '1' || key == '3')
            {
                if (GlobalArray != null)
                {
                    GlobalArray.fs.Dispose();
                }
                GlobalArray    = new MyFileArray("array.dat", size, seed);
                GlobalArray.fs = new FileStream("array.dat", FileMode.Open, FileAccess.ReadWrite);
            }
            if (key == '2' || key == '3')
            {
                if (GlobalList != null)
                {
                    GlobalList.fs.Dispose();
                }
                GlobalList    = new MyFileList("list.dat", size, seed);
                GlobalList.fs = new FileStream("list.dat", FileMode.Open, FileAccess.ReadWrite);
            }
        }
示例#2
0
        public static void Test_File_Array_List(int seed)
        {
            if (!Directory.Exists("tmp"))
            {
                Directory.CreateDirectory("tmp");
            }
            bool   print = false;
            long   startTime, endTime;
            int    n = 120;
            string filename;

            filename = @"mydataarray.dat";
            MyFileArray myfilearray = new MyFileArray(filename, n, seed);

            using (myfilearray.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
            {
                Console.WriteLine("\n FILE ARRAY \n");
                if (print)
                {
                    myfilearray.Print(n);
                }

                startTime = DateTime.Now.Ticks;
                BucketSort(myfilearray);
                endTime = DateTime.Now.Ticks;

                if (print)
                {
                    myfilearray.Print(n);
                }

                Console.WriteLine("Is sorted: " + myfilearray.IsSorted());
                Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));
            }
            filename = @"mydatalist.dat";
            MyFileList myfilelist = new MyFileList(filename, n, seed);

            using (myfilelist.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
            {
                Console.WriteLine("\n FILE LIST \n");
                if (print)
                {
                    myfilelist.Print(n);
                }

                startTime = DateTime.Now.Ticks;
                myfilelist.BucketSort();
                endTime = DateTime.Now.Ticks;

                if (print)
                {
                    myfilelist.Print(n);
                }

                Console.WriteLine("Is sorted: " + myfilelist.IsSorted());
                Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));
            }
        }
示例#3
0
        public void BucketSort()
        {
            MyFileList[] buckets = new MyFileList[length];
            for (int i = 0; i < buckets.Length; i++)
            {
                string file = "tmp/tmp" + i + ".dat";
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                buckets[i] = new MyFileList(file);
            }

            int node = ReadHead();

            while (node != 0)
            {
                int bi = (int)((double)Length * ReadValue(node));
                buckets[bi].Add(ReadValue(node));
                node = ReadNextNode(node);
            }

            foreach (var bucket in buckets)
            {
                bucket.InsertionSort();
            }

            node = ReadHead();
            for (int i = 0; i < Length; i++)
            {
                var bucket = buckets[i];
                if (bucket.Length == 0)
                {
                    continue;
                }
                WriteValue(node, bucket.Head());
                node = ReadNextNode(node);
                for (int j = 1; j < bucket.Length; j++)
                {
                    WriteValue(node, bucket.Next());
                    node = ReadNextNode(node);
                }
            }

            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i].Close();
            }
        }
示例#4
0
        public static void BucketSort(MyFileArray input)
        {
            MyFileList[] buckets = new MyFileList[input.Length];
            for (int i = 0; i < buckets.Length; i++)
            {
                string file = "tmp/tmp" + i + ".dat";
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                buckets[i] = new MyFileList(file);
            }
            for (int i = 0; i < input.Length; i++)
            {
                int bi = (int)((double)input.Length * input[i]);
                buckets[bi].Add(input[i]);
            }
            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i].InsertionSort();
            }
            int index = 0;

            for (int i = 0; i < input.Length; i++)
            {
                var bucket = buckets[i];
                if (bucket.Length == 0)
                {
                    continue;
                }
                input[index++] = bucket.Head();
                for (int j = 1; j < bucket.Length; j++)
                {
                    input[index++] = bucket.Next();
                }
            }
            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i].Close();
            }
        }
示例#5
0
        public static void Test_Array_List(int seed)
        {
            bool merge  = false;
            bool bucket = false;

            Console.WriteLine("Press M to MergeSort\nPress B to BucketSort\nPress Q to quit\n");
            bool input = true;

            while (input)
            {
                var key = Console.ReadKey();
                switch (char.ToUpper(key.KeyChar))
                {
                case 'M':
                    merge = true;
                    input = false;
                    Console.WriteLine("\n\nPerforming MergeSort...\n");
                    break;

                case 'B':
                    bucket = true;
                    input  = false;
                    Console.WriteLine("\n\nPerforming BucketSort...\n");
                    break;

                case 'Q':
                    return;
                }
            }

            System.Threading.Thread.Sleep(1000);

            if (merge && bucket)
            {
                Console.WriteLine("Cannot perform merge sort and bucket sort at the same time");
                return;
            }

            List <string> data = new List <string>();

            int[] amount    = { 100, 250, 500, 1000, 1500, 2000, 2500, 3000 };
            int   repeat    = 10;
            bool  print     = false;
            long  startTime = 0;
            long  endTime   = 0;

            // int n = 100000;
            data.Add("ARRAY\n");
            Console.WriteLine("\n ARRAY \n");
            foreach (int n in amount)
            {
                Console.WriteLine("n = " + n);
                for (int i = 0; i < repeat; i++)
                {
                    MyFileArray myarray = new MyFileArray("mydataarray.dat", n, seed);
                    using (myarray.fs = new FileStream("mydataarray.dat", FileMode.Open, FileAccess.ReadWrite))
                    {
                        if (print)
                        {
                            myarray.Print(n);
                        }
                        GC.Collect();
                        if (merge)
                        {
                            startTime = DateTime.Now.Ticks;
                            MergeSort(myarray);
                            endTime = DateTime.Now.Ticks;
                        }
                        if (bucket)
                        {
                            startTime = DateTime.Now.Ticks;
                            BucketSort(myarray);
                            endTime = DateTime.Now.Ticks;
                        }
                        if (print)
                        {
                            myarray.Print(n);
                        }
                        Console.WriteLine("Is sorted: " + myarray.IsSorted());
                    }
                    Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));

                    data.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                data.Add("");
            }

            int line = 2 + amount.Length + (repeat * amount.Length);
            int l    = 3;

            foreach (int n in amount)
            {
                data.Add($"{n};=AVERAGE(B{l}:B{l + repeat - 1})");
                l += repeat + 1;
            }

            data.Add("\n\n\nLIST\n");
            Console.WriteLine("\n LIST \n");
            foreach (int n in amount)
            {
                Console.WriteLine("n = " + n);
                for (int i = 0; i < repeat; i++)
                {
                    MyFileList mylist = new MyFileList("mydatalist.dat", n, seed);
                    using (mylist.fs = new FileStream("mydatalist.dat", FileMode.Open, FileAccess.ReadWrite))
                    {
                        if (print)
                        {
                            mylist.Print(n);
                        }
                        GC.Collect();
                        if (merge)
                        {
                            startTime = DateTime.Now.Ticks;
                            mylist.MergeSort();
                            endTime = DateTime.Now.Ticks;
                        }
                        if (bucket)
                        {
                            startTime = DateTime.Now.Ticks;
                            mylist.BucketSort();
                            endTime = DateTime.Now.Ticks;
                        }
                        if (print)
                        {
                            mylist.Print(n);
                        }
                        Console.WriteLine("Is sorted: " + mylist.IsSorted());
                    }
                    Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));
                    data.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                data.Add("");
            }
            l += 5 + amount.Length;
            foreach (int n in amount)
            {
                data.Add($"{n};=AVERAGE(B{l}:B{l + repeat - 1})");
                l += repeat + 1;
            }
            if (merge)
            {
                File.WriteAllLines("Merge_Times.csv", data);
            }
            if (bucket)
            {
                File.WriteAllLines("Bucket_Times.csv", data);
            }
        }