Esempio n. 1
0
        public static void BucketSort(DataArray input)
        {
            MyDataList[] buckets = new MyDataList[input.Length];
            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i] = new MyDataList();
            }
            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();
                }
            }
        }
Esempio n. 2
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')
            {
                GlobalArray = new MyDataArray(size, seed);
            }
            if (key == '2' || key == '3')
            {
                GlobalList = new MyDataList(size, seed);
            }
        }
Esempio n. 3
0
        public void BucketSort()
        {
            MyDataList[] buckets = new MyDataList[Length];
            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i] = new MyDataList();
            }
            MyLinkedListNode node = headNode;

            while (node != null)
            {
                int bi = (int)((double)Length * node.data);
                buckets[bi].Add(node.data);
                node = node.nextNode;
            }
            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i].InsertionSort();
            }
            node = headNode;
            for (int i = 0; i < Length; i++)
            {
                var bucket = buckets[i];
                if (bucket.Length == 0)
                {
                    continue;
                }
                node.data = bucket.Head();
                node      = node.nextNode;
                for (int j = 1; j < bucket.Length; j++)
                {
                    node.data = bucket.Next();
                    node      = node.nextNode;
                }
            }
        }
Esempio n. 4
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> dataTime = new List <string>();
            List <string> dataOps  = new List <string>();

            int[] amount    = { 100000, 150000, 200000, 250000, 300000, 400000, 500000, 750000, 1000000, 1250000, 1500000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000 };
            int   repeat    = 10;
            bool  print     = false;
            long  startTime = 0;
            long  endTime   = 0;

            // int n = 100000;
            dataTime.Add("ARRAY\n");
            dataOps.Add("ARRAY\n");
            Console.WriteLine("\n ARRAY \n");
            foreach (int n in amount)
            {
                Console.WriteLine("n = " + n);
                for (int i = 0; i < repeat; i++)
                {
                    MyDataArray myarray = new MyDataArray(n, seed);
                    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));
                    dataTime.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                if (merge)
                {
                    ulong operations = MergeSortOperations((ulong)n);
                    dataOps.Add($"{n};{operations}");
                }
                if (bucket)
                {
                    ulong operations = BucketSortOperations((ulong)n);
                    dataOps.Add($"{n};{operations}");
                }
                dataTime.Add("");
            }

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

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

            dataTime.Add("\n\n\nLIST\n");
            dataOps.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++)
                {
                    MyDataList mylist = new MyDataList(n, seed);
                    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));
                    dataTime.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                if (merge)
                {
                }
                if (bucket)
                {
                    ulong operations = BucketSortOperations((ulong)n);
                    dataOps.Add($"{n};{operations}");
                }
                dataTime.Add("");
            }
            l += 5 + amount.Length;
            foreach (int n in amount)
            {
                dataTime.Add($"{n};=AVERAGE(B{l}:B{l + repeat - 1})");
                l += repeat + 1;
            }
            if (merge)
            {
                File.WriteAllLines("Merge_Times.csv", dataTime);
                File.WriteAllLines("Merge_Operations.csv", dataOps);
            }
            if (bucket)
            {
                File.WriteAllLines("Bucket_Times.csv", dataTime);
                File.WriteAllLines("Bucket_Operations.csv", dataOps);
            }
        }