代码改变世界

算法——排序

2012-12-18 15:16  msfte  阅读(197)  评论(0编辑  收藏  举报

1,插入排序

    void InsertionSort(int[] array){}

        public void InsertionSort(int[] array)
        {
            int[] sortedArray = new int[array.Length];
            sortedArray[0] = array[0];
            for (int i = 1; i < array.Length; i++)
            {                
                for (int j = i - 1; j >= 0; j--)
                {
                    if (array[i] >= sortedArray[j])
                    {
                        sortedArray[j + 1] = array[i];
                        break;
                    }
                    else
                    {
                        sortedArray[j + 1] = sortedArray[j];
                        if (j == 0)
                            sortedArray[0] = array[i];
                    }
                }
            }
            for (int i = 0; i < array.Length; i++)
                array[i] = sortedArray[i];                
        }

 

2,归并排序

    void MergeSort(int[] array){}

void MergeSort(int[] array)
{
    MergeSortRecur(array,0,array.Length-1);
}

void MergeSortRecur(int[] array, int low, int high)
{
    if(low>=high)
      return;
    int middle = low + (high - low)/2;
    MergeSortRecur(array,low,middle);
    MergeSortRecur(array,middle+1,high);
    Merge(array,low,middle,high);
}

void Merge(int[] array,int low,int middle,int high)
{
    int[] tempArray = new int[array.Length];
    int index = low;
    int first = low;
    int second = middle+1;
    while(first<=middle&&second<=high)
    {
      if(array[first]>=array[second])
        tempArray[index++] = array[second++];
      else
        tempArray[index++] = array[first++];
    }
    while(first<=middle)
      tempArray[indxe++] = array[first++];
    while(second<=high)
      tempArray[index++] = array[second++];
}

 

3,快排

    void QuickSort(int[] array){}

   

void QuickSort(int[] array)
{
    QuickSortRecur(array,0,array.Length-1);
}

void QuickSortRecur(int[] array, int low, int high)
{
    if(low>=high)
      return
    int pivot = array[low];
    int littleIndex = low;
    for(int i = low;i<high;i++)
    {
      if(array[i]<pivot)   
        Swap(array,i,littleIndex+++1);
    }
    Swap(array,low,littleIndex);
    QuickSortRecur(array,low,littleIndex-1);
    QuickSortRecur(array,littleIndex+1,high);
}

void Swap(int[] array,int first,int second)
{
    int tempItem = array[first];
    array[first] = array[second];
    array[second] = temItem;
}
    

 

4,堆排

    void HeapSort(int[] array){}

void HeapSort(int[] array, int n)
        {
            MakeMinHeap(array, n);
            for (int i = n - 1; i >= 0; i--)
            {
                Swap(array[i], array[0]);
                MinHeapDown(array, 0, i);
            }
        }

        void Swap(int a, int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        void MinHeapDown(int[] array, int topIndex, int n)
        {
            int temp = array[topIndex];
            if (topIndex * 2 + 1 >= n)
                return;
            int leftChildIndex = topIndex * 2 + 1;
            int minChildIndex = leftChildIndex;
            if (topIndex * 2 + 2 < n)
            {
                int rightChildIndex = topIndex * 2 + 2;
                int right = array[rightChildIndex];
                minChildIndex = array[leftChildIndex] < array[rightChildIndex] ? leftChildIndex : rightChildIndex;
            }
            if (array[topIndex] <= array[minChildIndex])
                return;
            Swap(array[topIndex], array[minChildIndex]);
            MinHeapDown(array, minChildIndex, n);
        }

        void MakeMinHeap(int[] array, int n)
        {
            for (int i = (n - 1) / 2; i >= 0; i--)
            {
                MinHeapDown(array, i, n);
            }
        }

 李享的Heapsort:

        public void Heapsort(int[] array)
        {
            BuildMaxHeap(array);//O(nlogn)
            RemoveAndAdjustHeap(array);  //O(nlogn)          
        }

        private void RemoveAndAdjustHeap(int[] array)
        {
            for(int i = array.Length-1;i>0;i--)//O(n)
            {
                Swap(array, i, 0);
                AdjustHeap(array, 0,i);//O(logn)
            }            
        }

        private void BuildMaxHeap(int[] array)
        {
            for (int i = array.Length / 2 - 1; i >= 0; i--)//For every parent            
                AdjustHeap(array, i,array.Length);            
        }

        private void AdjustHeap(int[] array, int i,int length)//O(logn)
        {
            int biggerChildIndex = FindBiggerChild(array, i,length);
            if (biggerChildIndex >= 0)
            {
                if (array[i] < array[biggerChildIndex])
                {
                    Swap(array, i, biggerChildIndex);
                    AdjustHeap(array, biggerChildIndex,length);//T(n/2)
                }
            }
        }

        private int FindBiggerChild(int[] array,int i,int length)
        {
            int firstChildIndex = (i + 1) * 2 - 1;
            int secondChildIndex = (i + 1) * 2;            
            if (length <= firstChildIndex)
                return -1;
            else if (length == secondChildIndex)
                return firstChildIndex;
            else
                return array[firstChildIndex] > array[secondChildIndex] ? firstChildIndex : secondChildIndex;            
        }

        private void Swap(int[] array, int i, int biggerChildIndex)
        {
            int temp = array[i];
            array[i] = array[biggerChildIndex];
            array[biggerChildIndex] = temp;
        }

 

 

5,桶排

    void CountingSort(int[] array){}

 

6,希尔排序

void shellSort(int *array, int size){

    int stepLength = size/2;
        
    while (stepLength>=1){
            
        for(int i= stepLength;i<size;i++ ){
            int temp = array[i];                
            
            for(int j = i;j>=stepLength;j-=stepLength ){
                    
                if(array[j-stepLength]<=temp){
                    array[j] = temp;
                    break;
                }
                array[j] = array[j-stepLength];
                
                if(j-stepLength<stepLength)
                    array[j-stepLength] = temp;
            }

        }
        stepLength = stepLength/2;

    } 

}

 鸡尾酒排序

        static void CockTailSort(int[] array){

            int left = 0;
            int right = array.Length-1;
            for (int i = 0; left<right; i++)
            {
                for (int j = left; j < right; j++)
                {
                    if (array[j] > array[j + 1]) {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }                 
                }

                right--;

                for (int j = right; j > left; j--)
                {
                    if (array[j] < array[j - 1])
                    {
                        int temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }

                }

                left++;           
            }  
        }