Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Rotate Matrix;

            //int[,] matrix = new int[4,4] { { 1, 2,3,4}, { 5,6,7,8 },{ 9,10,11,12},{ 13,14,15,16} };

            //matrix=RotateMatrix(matrix);

            //for (int i = 0; i < matrix.GetLength(0); i++)
            //{
            //    for (int j = 0; j < matrix.GetLength(1); j++) {
            //        Console.Write(matrix[i,j].ToString()+"\t");
            //    }
            //    Console.WriteLine();
            //}

            //Sort Colors

            //char[] colors = new char[] { 'R','G','R','G','R','G'};
            //colors = SortColors(colors);

            //foreach (char ch in colors) {
            //    Console.WriteLine(ch.ToString());
            //}

            int[]             arr    = new int[] { 7, 10, 4, 3, 20, 15 };
            QuickSelectResult result = new QuickSelectResult();

            result.stop = false;

            Console.WriteLine(QuickSelect(arr, 0, arr.Length - 1, 2, result).number.ToString());

            Console.Read();
        }
Ejemplo n.º 2
0
        public static QuickSelectResult QuickSelect(int[] arr, int left, int right, int k, QuickSelectResult result)
        {
            if (left <= right)
            {
                int pi = Partition(arr, left, right);

                if (pi + 1 == k)
                {
                    result.stop   = true;
                    result.number = arr[pi];
                }

                if (!result.stop)
                {
                    result = QuickSelect(arr, left, pi - 1, k, result);
                }
                if (!result.stop)
                {
                    result = QuickSelect(arr, pi + 1, right, k, result);
                }

                return(result);
            }
            return(result);
        }