PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/doc/Dissertation/figs/QuickSort_original.cs

http://github.com/icsharpcode/ILSpy
C# | 44 lines | 44 code | 0 blank | 0 comment | 5 complexity | 8690a68e189b1b683da1d8b2e232ef26 MD5 | raw file
 1using System;
 2static class QuickSortProgram
 3{
 4    public static void Main(string[] args)
 5    {
 6        int[] intArray = new int[args.Length];
 7        for (int i = 0; i < intArray.Length; i++) {
 8            intArray[i] = int.Parse(args[i]);
 9        }
10        QuickSort(intArray, 0, intArray.Length - 1);
11        for (int i = 0; i < intArray.Length; i++) {
12            Console.Write(intArray[i].ToString() + " ");
13        }
14    }
15    public static void QuickSort(int[] array, int left, int right)
16    {
17        if (right > left) {
18            int pivotIndex = (left + right) / 2;
19            int pivotNew = Partition(array, left, right, pivotIndex);
20            QuickSort(array, left, pivotNew - 1);
21            QuickSort(array, pivotNew + 1, right);
22        }
23    }
24    static int Partition(int[] array, int left, int right, int pivotIndex)
25    {
26        int pivotValue = array[pivotIndex];
27        Swap(array, pivotIndex, right);
28        int storeIndex = left;
29        for(int i = left; i < right; i++) {
30            if (array[i] <= pivotValue) {
31                Swap(array, storeIndex, i);
32                storeIndex = storeIndex + 1;
33            }
34        }
35        Swap(array, right, storeIndex);
36        return storeIndex;
37    }
38    static void Swap(int[] array, int index1, int index2)
39    {
40        int tmp = array[index1];
41        array[index1] = array[index2];
42        array[index2] = tmp;
43    }
44}