mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
static class QuickSortProgram |
|
{ |
|
public static void Main(string[] args) |
|
{ |
|
int[] intArray = new int[args.Length]; |
|
for (int i = 0; i < intArray.Length; i++) { |
|
intArray[i] = int.Parse(args[i]); |
|
} |
|
QuickSort(intArray, 0, intArray.Length - 1); |
|
for (int i = 0; i < intArray.Length; i++) { |
|
System.Console.Write(intArray[i].ToString() + " "); |
|
} |
|
} |
|
|
|
/// For description of this algorithm see: |
|
/// http://en.wikipedia.org/wiki/Quick_sort |
|
public static void QuickSort(int[] array, int left, int right) |
|
{ |
|
if (right > left) { |
|
int pivotIndex = (left + right) / 2; |
|
int pivotNew = Partition(array, left, right, pivotIndex); |
|
QuickSort(array, left, pivotNew - 1); |
|
QuickSort(array, pivotNew + 1, right); |
|
} |
|
} |
|
|
|
static int Partition(int[] array, int left, int right, |
|
int pivotIndex) |
|
{ |
|
int pivotValue = array[pivotIndex]; |
|
Swap(array, pivotIndex, right); |
|
int storeIndex = left; |
|
for(int i = left; i < right; i++) { |
|
if (array[i] <= pivotValue) { |
|
Swap(array, storeIndex, i); |
|
storeIndex = storeIndex + 1; |
|
} |
|
} |
|
Swap(array, right, storeIndex); |
|
return storeIndex; |
|
} |
|
|
|
static void Swap(int[] array, int index1, int index2) |
|
{ |
|
int tmp = array[index1]; |
|
array[index1] = array[index2]; |
|
array[index2] = tmp; |
|
} |
|
} |