- public void QuickSort(int[] elems)
- {
- foreach (int elem in elems)
- Console.Write(elem + " ");
- Console.WriteLine();
- Console.WriteLine("---------------------------------------");
- QuickSort(0,elems.Length-1,elems);
- Console.WriteLine("---------------------------------------");
- foreach (int elem in elems)
- Console.Write(elem + " ");
- Console.WriteLine();
- }
- public void QuickSort(int left, int right, int[] elems)
- {
- if (left < right)
- {
- int index = Partition(left, right, elems);
- QuickSort(left, index - 1, elems);
- QuickSort(index+1, right, elems);
- }
- }
- private int Partition(int left, int right, int[] elems)
- {
- int temp = elems[left];
- while (left < right)
- {
- while (left < right && elems[right] >= temp)
- {
- right--;
- }
- elems[left] = elems[right];
- while (left < right && elems[left] <= temp)
- {
- left++;
- }
- elems[right] = elems[left];
- }
- elems[left] = temp;
- return left;
- }