Пример #1
0
        public static void MainTest(string[] args)
        {
            int n = int.Parse(args[0]);

            if (args.Length == 2)
            {
                StdRandom.Seed = int.Parse(args[1]);
            }
            double[] probabilities = { 0.5, 0.3, 0.1, 0.1 };
            int[]    frequencies   = { 5, 3, 1, 1 };
            string[] a             = "A B C D E F G".Split(' ');

            Console.WriteLine("seed = " + StdRandom.Seed);
            for (int i = 0; i < n; i++)
            {
                Console.Write("{0} ", StdRandom.Uniform(100));
                Console.Write("{0:F5} ", StdRandom.Uniform(10.0, 99.0));
                Console.Write("{0} ", StdRandom.Bernoulli(0.5));
                Console.Write("{0:F5} ", StdRandom.Gaussian(9.0, 0.2));
                Console.Write("{0} ", StdRandom.Discrete(probabilities));
                Console.Write("{0} ", StdRandom.Discrete(frequencies));
                StdRandom.Shuffle(a);

                foreach (string s in a)
                {
                    Console.Write(s);
                }
                Console.WriteLine();
            }
        }
Пример #2
0
        /// <summary>
        /// Rearranges the array so that a[k] contains the kth smallest key;
        /// a[0] through a[k-1] are OrderHelper.Less than (or equal to) a[k]; and
        /// a[k+1] through a[N-1] are greater than (or equal to) a[k].</summary>
        /// <param name="a">a the array</param>
        /// <param name="k">k find the kth smallest</param>
        /// <returns>the rearranged array</returns>
        /// <exception cref="IndexOutOfRangeException"> if k is out of range</exception>
        ///
        public static IComparable Select(IComparable[] a, int k)
        {
            if (a == null)
            {
                throw new ArgumentNullException(nameof(a), "The array cannot be null");
            }
            if (k < 0 || k >= a.Length)
            {
                throw new IndexOutOfRangeException("Selected element out of bounds");
            }
            StdRandom.Shuffle(a);
            int lo = 0, hi = a.Length - 1;

            while (hi > lo)
            {
                int i = Partition(a, lo, hi);
                if (i > k)
                {
                    hi = i - 1;
                }
                else if (i < k)
                {
                    lo = i + 1;
                }
                else
                {
                    return(a[i]);
                }
            }
            return(a[lo]);
        }
Пример #3
0
        // TODO: Add a generic verion

        /// <summary>
        /// Rearranges the array in ascending order, using the natural order.</summary>
        /// <param name="a">a the array to be sorted</param>
        ///
        public static void Sort(IComparable[] a)
        {
            if (a == null)
            {
                throw new ArgumentNullException(nameof(a), "The array to be sorted cannot be null");
            }

            StdRandom.Shuffle(a);
            Sort(a, 0, a.Length - 1);
            Debug.Assert(OrderHelper.IsSorted(a));
        }
Пример #4
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            string[] a = StdIn.ReadAllStrings();
            OrderHelper.Show(a);

            // shuffle
            StdRandom.Shuffle(a);

            // display results again using select
            Console.WriteLine();
            for (int i = 0; i < a.Length; i++)
            {
                string ith = (string)Quick.Select(a, i);
                Console.WriteLine(ith);
            }
        }
Пример #5
0
 /// <summary>
 /// Rearranges the array in ascending order, using the natural order.</summary>
 /// <param name="a">the array to be sorted</param>
 ///
 public static void Sort(IComparable[] a)
 {
     StdRandom.Shuffle(a);
     Sort(a, 0, a.Length - 1);
     Debug.Assert(OrderHelper.IsSorted(a));
 }