All the classic C # sorting algorithm Summary

1, selection sort 

Selection Sort
class SelectionSorter    
{    
    private int min;    
    public void Sort(int[] arr)    
    {    
        for (int i = 0; i < arr.Length - 1; ++i)    
        {    
            min = i;    
            for (int j = i + 1; j < arr.Length; ++j)    
            {    
                if (arr[j] < arr[min])    
                    min = j;    
            }    
            you are t = arr [min];    
            arr[min] = arr[i];    
            arr[i] = t;    
        }    
    }    
} 

2, bubble sort

Bubble Sort
class EbullitionSorter    
{    
    public void Sort(int[] arr)    
    {    
        int i, j, temp;    
        bool done = false;    
        j = 1;    
        while ((j < arr.Length) && (!done))//判断长度    
        {    
            done = true;    
            for (i = 0; i < arr.Length - j; i++)    
            {    
                if (arr[i] > arr[i + 1])    
                {    
                    done = false;    
                    temp = arr[i];    
                    arr [i] = arr [i + 1]; // data exchange    
                    arr[i + 1] = temp;    
                }    
            }    
            j++;    
        }    
    }      
} 

3, Quick Sort

Quick Sort
class QuickSorter    
{    
    private void swap(ref int l, ref int r)    
    {    
        int temp;    
        temp = l;    
        l = r;    
        r = temp;    
    }    
    public void Sort(int[] list, int low, int high)    
    {    
        int pivot; // storage branch point    
        you are, r;    
        int mid;    
        if (high <= low)    
            return;    
        else if (high == low + 1)    
        {    
            if (list[low] > list[high])    
                swap(ref list[low], ref list[high]);    
            return;    
        }    
        mid = (low + high) >> 1;    
        pivot = list[mid];    
        swap(ref list[low], ref list[mid]);    
        l = low + 1;    
        r = high;    
        do   
        {    
        while (l <= r && list[l] < pivot)    
            l++;    
        while (list[r] >= pivot)    
            r--;    
            if (l < r)    
                swap(ref list[l], ref list[r]);    
        } while (l < r);    
        list[low] = list[r];    
        list[r] = pivot;    
        if (low + 1 < r)    
            Sort(list, low, r - 1);    
        if (r + 1 < high)    
            Sort(list, r + 1, high);    
    }      
}    

4, insertion sort 

Insertion Sort 
public class InsertionSorter    
{    
    public void Sort(int[] arr)    
    {    
        for (int i = 1; i < arr.Length; i++)    
        {    
            int t = arr[i];    
            int j = i;    
            while ((j > 0) && (arr[j - 1] > t))    
            {    
                arr [j] = arr [j - 1]; // exchange sequence    
                --j;    
            }    
            arr[j] = t;    
        }    
    }     
}    

5, Hill sorting 

Shell sort
public class ShellSorter    
{    
    public void Sort(int[] arr)    
    {    
        int inc;    
        for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;    
        for (; inc > 0; inc /= 3)    
        {    
            for (int i = inc + 1; i <= arr.Length; i += inc)    
            {    
                int t = arr[i - 1];    
                int j = i;    
                while ((j > inc) && (arr[j - inc - 1] > t))    
                {    
                    arr [j - 1] = arr [j - inc - 1]; // data exchange    
                    j -= inc;    
                }    
                arr[j - 1] = t;    
            }    
        }    
    }   
}   

6, merge sort

Merge sort
        /// <summary>
        /// merge sort of return: merge sort entrance
        /// </summary>
        /// <param name = "data"> unordered array </ param>
        /// <returns> ordered array </ returns>
        /// <author>Lihua(www.zivsoft.com)</author>
        int[] Sort(int[] data)
        {
            // get the array index intermediate
            int middle = data.Length / 2;
            // Initialize the temporary array let, right, and define the result as the final ordered array
            int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
            if (data.Length% 2! = 0) // if an odd number of array elements, the right temporary array reinitialization
            {
                right = new int[middle + 1];
            }
            if (data.Length <= 1) // only 1 or 0 th metadata, return unordered
            {
                return data;
            }
            int i = 0, j = 0;
            foreach (int x in data) // start sorting
            {
                if (i <middle) // padding-left array
                {
                    left[i] = x;
                    i++;
                }
                else // fill an array of Right
                {
                    right[j] = x;
                    j++;
                }
            }
            left = Sort (left); // array of left recursion
            right = Sort (right); // recursive array Right
            result = Merge (left, right); // start sorting
            //this.Write(result);// output sequencing, test (lihua debug)
            return result;
        }
        /// <summary>
        /// and merge sort of: sorting step in this
        /// </summary>
        /// <param name = "a"> Left array </ param>
        /// <param name = "b"> Right array </ param>
        /// <returns> merge the sorted array around Back </ returns>
        int[] Merge(int[] a, int[] b)
        {
            // definition of the resulting array, for storing the final result
            int[] result = new int[a.Length + b.Length];
            int i = 0, j = 0, k = 0;
            while (i < a.Length && j < b.Length)
            {
                if (a [i] <b [j]) // less than the left and right elements in the array elements in the array
                {
                    result [k ++] = a [i ++]; // put the result that a small array
                }
                else // Left elements in the array is greater than the right elements in the array
                {
                    result [k ++] = b [j ++]; // put the result that a small array
                }
            }
            while (i <a.Length) // this is actually also left element, but not the right elements
            {
                result[k++] = a[i++];
            }
            while (j <b.Length) // right right elements, no elements left
            {
                result[k++] = b[j++];
            }
            return result; // return the result array
        }
Note: This algorithm is provided by ZHOU Li-hua (http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html 

7, radix sort

Radix Sort
        // radix sort
        public int[] RadixSort(int[] ArrayToSort, int digit)
        {   
            //low to high digit
            for (int k = 1; k <= digit; k++)
            {       
                //temp array to store the sort result inside digit
                int[] tmpArray = new int[ArrayToSort.Length]; 
                //temp array for countingsort 
                int[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};        
                //CountingSort        
                for (int i = 0; i < ArrayToSort.Length; i++)        
                {           
                    //split the specified digit from the element 
                    int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10; 
                    tmpCountingSortArray[tmpSplitDigit] += 1; 
                }         
                for (int m = 1; m < 10; m++)      
                {            
                    tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];        
                }        
                //output the value to result      
                for (int n = ArrayToSort.Length - 1; n >= 0; n--)       
                {           
                    int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k - 1) - (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;           
                    tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];            
                    tmpCountingSortArray[tmpSplitDigit] -= 1;       
                }        
                //copy the digit-inside sort result to source array       
                for (int p = 0; p < ArrayToSort.Length; p++)       
                {           
                    ArrayToSort[p] = tmpArray[p];       
                }   
            }    
            return ArrayToSort;
        }

8, counting sequencing

Counting Sort
// count Sort
        /// <summary>
        /// counting sort
        /// </summary>
        /// <param name="arrayA">input array</param>
        /// <param name="arrange">the value arrange in input array</param>
        /// <returns></returns>
        public int[] CountingSort(int[] arrayA, int arrange)
        {    
            //array to store the sorted result,  
            //size is the same with input array. 
            int[] arrayResult = new int[arrayA.Length];    
            //array to store the direct value in sorting process   
            //include index 0;    
            //size is arrange+1;    
            int[] arrayTemp = new int[arrange+1];    
            //clear up the temp array    
            for(int i = 0; i <= arrange; i++)    
            {        
                arrayTemp[i] = 0;  
            }    
            //now temp array stores the count of value equal  
            for(int j = 0; j < arrayA.Length; j++)   
            {       
                arrayTemp[arrayA[j]] += 1;   
            }    
            //now temp array stores the count of value lower and equal  
            for(int k = 1; k <= arrange; k++)   
            {       
                arrayTemp[k] += arrayTemp[k - 1];  
            }     
            //output the value to result    
            for (int m = arrayA.Length-1; m >= 0; m--)   
            {        
                arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];    
                arrayTemp[arrayA[m]] -= 1;  
            }    
            return arrayResult;
        }


9, a small root heapsort

Small root heapsort
/// <summary>
        /// small root heapsort
        /// </summary>
        /// <param name="dblArray"></param>
        /// <param name="StartIndex"></param>
        /// <returns></returns>

        private void HeapSort(ref double[] dblArray)
        {
            for (int i = dblArray.Length - 1; i >= 0; i--)
            {
                if (2 * i + 1 < dblArray.Length)
                {
                    int MinChildrenIndex = 2 * i + 1;
                    // Compare the left subtree and right subtree, minimum recording Index
                    if (2 * i + 2 < dblArray.Length)
                    {
                        if (dblArray[2 * i + 1] > dblArray[2 * i + 2])
                            MinChildrenIndex = 2 * i + 2;
                    }
                    if (dblArray[i] > dblArray[MinChildrenIndex])
                    {


                        ExchageValue(ref dblArray[i], ref dblArray[MinChildrenIndex]);
                        NodeSort(ref dblArray, MinChildrenIndex);
                    }
                }
            }
        }

        /// <summary>
        /// node sorting
        /// </summary>
        /// <param name="dblArray"></param>
        /// <param name="StartIndex"></param>

        private void NodeSort(ref double[] dblArray, int StartIndex)
        {
            while (2 * StartIndex + 1 < dblArray.Length)
            {
                int MinChildrenIndex = 2 * StartIndex + 1;
                if (2 * StartIndex + 2 < dblArray.Length)
                {
                    if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2])
                    {
                        MinChildrenIndex = 2 * StartIndex + 2;
                    }
                }
                if (dblArray[StartIndex] > dblArray[MinChildrenIndex])
                {
                    ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);
                    StartIndex = MinChildrenIndex;
                }
            }
        }

        /// <summary>
        /// exchange value
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        private void ExchageValue(ref double A, ref double B)
        {
            double Temp = A;
            A = B;
            B = Temp;
        }

  

Guess you like

Origin www.cnblogs.com/fengyeqingxiang/p/11021852.html