Exemplo n.º 1
0
 public MyArray(MyArray array)
 {
     for (int i = 0; i < array.length; i++)
     {
         data[i] = array.length;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Counting sort for array
        /// </summary>
        /// <param name="items">Array</param>
        public static void CountSort(DataArray items)
        {
            if (items == null)
            {
                return;
            }

            MyArray output   = new MyArray(items.Length);
            int     minValue = items[0];
            int     maxValue = items[0];

            //Finds min and max values
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] < minValue)
                {
                    minValue = items[i];
                }
                if (items[i] > maxValue)
                {
                    maxValue = items[i];
                }
            }

            //Counts frequencies
            int[] counts = new int[maxValue - minValue + 2];
            for (int i = 0; i < items.Length; i++)
            {
                counts[items[i] - minValue]++;
            }

            //Each element stores the sum of previous counts
            counts[0]--;
            for (int i = 1; i < counts.Length; i++)
            {
                counts[i] += counts[i - 1];
            }

            //Puts each element from items array to the right place using counts saved index
            for (int i = 0; i < items.Length; i++)
            {
                output[counts[items[i] - minValue]--] = items[i];
            }

            //Copies output to object
            for (int i = 0; i < items.Length; i++)
            {
                items[i] = output[i];
            }
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
            int n    = 10;
            //Array sorting
            DataArray data = new MyArray(n, seed);

            Console.WriteLine("[ARRAY] Counting sort");
            data.Print(data.Length);
            CountSort(data);
            data.Print(data.Length);

            //Linked list sorting
            DataList listData = new MyLinkedList(n, seed);

            Console.WriteLine("[List] Counting sort");
            listData.Print(listData.Length);
            CountSort(listData);
            listData.Print(listData.Length);

            Console.ReadKey();
        }