Search This Blog

Wednesday, May 9, 2012

Quick Sort in C#

Quick Sort in C#

public List<int> QuickSortAction(List<int> input, int left, int right)
        {         
            int i = left;
            int j = right;
            int pivotValue = input[(left + right) / 2];
            int temp = 0;

            while (i <= j)
            {
                while (input[i] < pivotValue)
                {
                    i++;
                }
                while (pivotValue < input[j])
                {
                    j--;
                }
                if (i <= j)
                {
                    temp = input[i];
                    input[i++] = input[j];
                    input[j--] = temp;
                }
            }

            if (left < j)
            {
                QuickSortAction(input, left, j);
            }

            if (i < right)
            {
                QuickSortAction(input, i, right);
            }

            return input;
        }

No comments:

Post a Comment