예제 #1
0
        //Arranging the elements
        public static void arrange(int k, int n)
        {
            Postman_Sort p = new Postman_Sort();
            int          i, j;

            for (i = k; i < (n - 1); i++)
            {
                for (j = i + 1; j < n; j++)
                {
                    if (arr1[i] > arr1[j])
                    {
                        p.temp  = arr1[i];
                        arr1[i] = arr1[j];
                        arr1[j] = p.temp;
                        p.temp  = (arr[i] % 10);
                        arr[i]  = (arr[j] % 10);
                        arr[j]  = p.temp;
                    }
                }
            }
        }
예제 #2
0
        public static void Main(string[] args)
        {
            Postman_Sort p = new Postman_Sort();
            int          t1, t, n = 1, i, j;

            Console.WriteLine("Enter the Size of the array ");
            p.count = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the elements of the array :-");
            for (i = 0; i < p.count; i++)
            {
                Console.Write("Element - {0} : ", i);
                arr[i]  = Convert.ToInt32(Console.ReadLine());
                arr1[i] = arr[i];
            }

            //Finding the longest digits in the array and no of elements in that digit
            for (i = 0; i < p.count; i++)
            {
                t = arr[i];
                while (t > 0)
                {
                    p.c++; //Counting the no of elements of a digit
                    t = t / 10;
                }
                if (p.maxdigits < p.c)
                {
                    p.maxdigits = p.c; //Storing the length of longest digit
                }
                p.c = 0;
            }
            for (i = 1; i < p.maxdigits; i++)
            {
                n = n * 10;
            }

            for (i = 0; i < p.count; i++)
            {
                p.max = arr[i] / n; //Dividing by a particular base
                t     = i;
                for (j = i + 1; j < p.count; j++)
                {
                    if (p.max > (arr[j] / n))
                    {
                        p.max = arr[j] / n;
                        t     = j;
                    }
                }
                p.temp  = arr1[t];
                arr1[t] = arr1[i];
                arr1[i] = p.temp;
                p.temp  = arr[t];
                arr[t]  = arr[i];
                arr[i]  = p.temp;
            }
            while (n >= 1)
            {
                for (i = 0; i < p.count;)
                {
                    t1 = arr[i] / n;
                    for (j = i + 1; t1 == (arr[j] / n); j++)
                    {
                        ;
                    }
                    arrange(i, j);
                    i = j;
                }
                n = n / 10;
            }
            Console.WriteLine("Sorted Array");
            for (i = 0; i < p.count; i++)
            {
                Console.Write("{0}  ", arr1[i]); //Displaying the Sorted array
            }
        }