C#排序算法汇集

  1.  AbstractSort
    using  System;

    namespace  Core
    {
        
    internal abstract class AbstractSort
        
    {
            
    protected int[] list;

            
    public event SortingDelegate GreenDataChangedEvent;
            
    public event SortingDelegate RedDataChangedEvent;
            
    public event SortingDelegate DataChangedEvent;

            
    protected bool stop= false;

            
    public bool Stop 
            
    {
                
    get return stop; }
                
    set { stop = value; }
            }


            
    public abstract void Sort(int[] list);

            
    protected void GreenDataChanged(int newIndex)
            
    {
                
    if(this.GreenDataChangedEvent != null)
                    GreenDataChangedEvent(newIndex);
            }


            
    protected void RedDataChanged(int newIndex)
            
    {
                
    if(this.RedDataChangedEvent != null)
                    RedDataChangedEvent(newIndex);
            }


            
    protected void DataChanged(int newIndex)
            
    {
                
    if(this.DataChangedEvent != null)
                    DataChangedEvent(newIndex);
            }


            
    protected void swap(int pos1, int pos2)
            
    {
                
    int temp = list[pos1];
                list[pos1] 
    = list[pos2];
                list[pos2] 
    = temp;

                DataChanged(pos1);
                DataChanged(pos2);
            }


            
    protected void Finished()
            
    {
                GreenDataChanged(
    -1);
                RedDataChanged(
    -1);
            }

        }

    }

  2. BubbleSort2
    using  System;

    namespace  Core
    {
        
    internal class BubbleSort2 : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;

                
    for(int i=0; i<list.Length - 1; i++)
                    
    for(int j=list.Length - 1; j>i; j--)
                    
    {
                        
    if(stop)
                            
    return;

                        
    if(list[i]>list[j])
                        
    {
                            GreenDataChanged(i);
                            RedDataChanged(j);
                            swap(i,j);
                        }

                    }


                Finished();
            }

        }

    }


    /********************************************
     * 注意!
     * 
     * 在使用循环嵌套时,内层循环一定是递减顺序,
     * 如果写反,将导致排序效率降低。
     * 
     * 用户可尝试将上面代码中的循环嵌套改为:
     * 
     * for(int i=0; i<list.Length - 1; i++)
     *      for(int j = i; j < list.Length; j++)
     *   {
     *     ......
     *   }
     * 
     * 观察排序性能收到的影响。
     *     
     ******************************************
    */
  3. BubbleSort
    using  System;

    namespace  Core
    {
        
    internal class BubbleSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;

                
    bool change = true;
                
    for (int i = list.Length-1; i>0 && change; --i) 
                
    {
                    change 
    = false;
                    
    for (int j=0; j<i; ++j)
                    
    {
                        
    if(stop)
                            
    return;

                        
    if (list[j] > list[j+1])
                        
    {
                            GreenDataChanged(j);
                            RedDataChanged(j
    +1);
                            swap(j,j
    +1);
                            change 
    = true;
                        }

                    }

                }


                Finished();
            }

        }

    }

  4. HeapSort
    using  System;

    namespace  Core
    {
        
    internal class HeapSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;
                
    int lastOutOfOrder;

                buildHeap();

                
    for(lastOutOfOrder = list.Length - 1; lastOutOfOrder >= 0;
                    lastOutOfOrder
    --)
                
    {
                    
    if(stop)
                        
    return;

                    swap(
    0, lastOutOfOrder);
                    heapify(
    0, lastOutOfOrder - 1);
                }


                Finished();
            }


            
    private void buildHeap()
            
    {
                
    int index;

                
    for(index = list.Length / 2 - 1; index >= 0; index--)
                
    {
                    
    if(stop)
                        
    return;

                    heapify(index, list.Length 
    - 1);
                }

            }


            
    private void heapify(int low, int high)
            
    {
                
    int largeIndex;

                
    int temp = list[low]; //copy the root node of the subtree

                largeIndex 
    = 2 * low + 1;  //index of the left child

                
    while(largeIndex <= high)
                
    {
                    
    if(stop)
                        
    return;

                    
    if(largeIndex < high)
                        
    if(list[largeIndex]<list[largeIndex + 1])
                            largeIndex 
    = largeIndex + 1//index of the
                    
    //largest child

                    
    if(temp>list[largeIndex]) //subtree is
                        
    //already in a heap
                        break;
                    
    else
                    
    {
                        list[low] 
    = list[largeIndex]; //move the larger child
                        GreenDataChanged(low);

                        
    //to the root
                        low = largeIndex;   //go to the subtree to
                        
    //restore the heap
                        largeIndex = 2 * low + 1;
                    }

                }


                list[low] 
    = temp; //insert temp into the tree, that is, list
                RedDataChanged(low);
            }
     
        }

    }

  5. InsertionSort
    using  System;

    namespace  Core
    {
        
    internal class InsertionSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;

                
    int unsortedIndex, location;
                
    int temp;

                
    for(unsortedIndex = 1; unsortedIndex < list.Length; unsortedIndex++)
                
    {
                    
    if(list[unsortedIndex] <list[unsortedIndex - 1])
                    
    {
                        temp 
    = list[unsortedIndex];
                        location 
    = unsortedIndex;

                        
    while(location > 0 && list[location - 1]>temp)
                        
    {
                            
    if(stop)
                                
    return;

                            list[location] 
    = list[location - 1];

                            
    if(location == unsortedIndex)
                                RedDataChanged(location);
                            
    else
                                GreenDataChanged(location);

                            location
    --;
                        }


                        list[location] 
    = temp;
                        DataChanged(location);
                    }

                }


                Finished();
            }

        }

    }

  6. LRadixSort
    using  System;

    namespace  Core
    {
        
    internal class Node
        
    {
            
    public int info;
            
    public Node link;

            
    public Node(int info)
            
    {
                
    this.info = info;
            }

        }


        
    internal class Radix
        
    {
            
    public Node first = null;
            
    public Node last = null;
            
    public int count = 0;

            
    public void AddNode(Node newNode)
            
    {
                
    if(first == null)
                
    {
                    first 
    = newNode;
                    last 
    = newNode;
                }

                
    else
                
    {
                    last.link 
    = newNode;
                    last 
    = newNode;
                }

                count
    ++;
            }


            
    public void RemoveFirst()
            
    {
                
    if(first == null)
                    
    return;

                
    if(first == last)
                
    {
                    first 
    = null;
                    last 
    = null;
                    count 
    = 0;
                }

                
    else
                
    {
                    first 
    = first.link;
                    count
    --;
                }

            }

        }


        
    // 基数排序法
        internal class LRadixSort : RadixSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    throw new Exception("Wrong method call!");
            }


            
    public override void Sort(int[] list, Radix[] radix)
            
    {
                
    this.list = list;
                
    this.radix = radix;

                
    for(int i=0; i<10; i++)
                    radix[i] 
    = new Radix();

                
    // 按最低位优先依次对各关键字进行分配和收集
                for(int i=0; i<2; i++)
                
    {
                    RadixStatusChanged(RadixSortStatus.Begin);
                    Distribute(i);
                    
                    
    if(stop)
                        
    return;

                    Collect();

                    
    if(stop)
                        
    return;
                }


                RadixStatusChanged(RadixSortStatus.Finished);
            }


            
    private void Distribute(int digitPlace)
            
    {
                
    int digit;

                digitPlace 
    = (int)(Math.Pow(10, digitPlace));

                
    for(int i=0; i<list.Length; i++)
                
    {
                    
    if(stop)
                        
    return;

                    
    if(digitPlace == 0)
                        digit 
    = list[i] - list[i]/10*10;
                    
    else
                        digit 
    = list[i]/digitPlace - list[i]/digitPlace/10*10;

                    radix[digit].AddNode(
    new Node(list[i]));
                    RadixDataChanged(digit, radix[digit].count);
                }

            }


            
    private void Collect()
            
    {
                
    int position = 0;
                Node current;

                
    for(int i=0; i<10; i++)
                
    {
                    current 
    = radix[i].first;
                    
    while(current != null)
                    
    {
                        
    if(stop)
                            
    return;

                        list[position] 
    = current.info;
                        RadixDataCollecting(i, radix[i].count);
                        radix[i].RemoveFirst();
                        position 
    ++;
                        current 
    = radix[i].first;
                    }

                }

            }

        }

    }
  7. MergeSort
    using  System;

    namespace  Core
    {
        
    internal class MergeSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;
                recMergeSort(
    0, list.Length - 1);

                
    if(stop)
                    
    return;

                Finished();
            }


            
    private void recMergeSort(int begin, int end)
            
    {
                
    if(end > begin)
                
    {
                    
    int middle = (end + begin)/2;
                    recMergeSort(begin, middle);
                    recMergeSort(middle 
    + 1, end);
                    Merge(begin, middle, end);

                    
    if(stop)
                        
    return;
                }

            }


            
    private void Merge(int begin, int middle, int end)
            
    {
                
    if(stop)
                    
    return;

                
    int first1 = begin;      //第一个数组段的当前指针
                int first2 = middle + 1//第二个数组段的当前指针

                
    int[] temp = new int[end - begin + 1];  // 临时存储单元
                int current = 0;

                
    //开始归并处理
                while(first1 != middle + 1 && first2 != end + 1)
                
    {
                    
    if(stop)
                        
    return;

                    
    if(list[first1] < list[first2])
                    
    {
                        temp[current] 
    = list[first1];
                        GreenDataChanged(first1);
                        
    if(stop)
                            
    return;
                        first1 
    ++;
                    }

                    
    else
                    
    {
                        temp[current] 
    = list[first2];
                        RedDataChanged(first2);
                        
    if(stop)
                            
    return;
                        first2 
    ++;
                    }

                    current 
    ++ ;
                }


                
    //处理归并后留下的“尾巴”
                if(first1 == middle + 1)  //前半部分首先归并完
                {
                    
    while(current < temp.Length)
                    
    {
                        temp[current] 
    = list[first2];
                        RedDataChanged(first2);
                        
    if(stop)
                            
    return;
                        first2
    ++;
                        current
    ++;
                    }

                }

                
    else  //后半部分首先归并完
                {
                    
    while(current < temp.Length)
                    
    {
                        temp[current] 
    = list[first1];
                        GreenDataChanged(first1);
                        
    if(stop)
                            
    return;
                        first1
    ++;
                        current
    ++;
                    }

                }


                
    //将temp中的结果记录进list当中
                for(int i=0; i<temp.Length; i++)
                
    {
                    list[begin 
    + i] = temp[i];
                    DataChanged(begin 
    + i);
                    
    if(stop)
                        
    return;
                }

            }

        }

    }

  8. QuickSort
    using  System;

    namespace  Core
    {
        
    internal class QuickSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;

                recQuickSort(
    0, list.Length - 1);

                Finished();
            }


            
    private void recQuickSort(int first, int last)
            
    {
                
    int pivotLocation;

                
    if(stop)
                    
    return;

                
    if(first < last)
                
    {
                    pivotLocation 
    = partition(first, last);
                    recQuickSort(first, pivotLocation 
    - 1);
                    recQuickSort(pivotLocation 
    + 1, last);
                }

            }


            
    private int partition(int first, int last)
            
    {
                
    int pivot;

                
    int index, smallIndex;

                swap(first, (first 
    + last) / 2);

                pivot 
    = list[first];
                smallIndex 
    = first;

                
    for(index = first + 1; index <= last; index++)
                
    {
                    
    if(stop)
                        
    return smallIndex;

                    
    if(list[index] < pivot)
                    
    {
                        smallIndex
    ++;

                        GreenDataChanged(smallIndex);
                        RedDataChanged(index);
                        swap(smallIndex, index);
                    }

                }


                GreenDataChanged(first);
                RedDataChanged(smallIndex);
                swap(first, smallIndex);

                
    return smallIndex;
            }

        }

    }

  9. RadixSort
    using  System;

    namespace  Core
    {
        
    internal enum RadixSortStatus
        
    {
            Begin,
            Finished
        }


        
    internal abstract class RadixSort : AbstractSort
        
    {
            
    protected Radix[] radix = new Radix[10];

            
    public event RadixSortingDelegate RadixDataChangedEvent;
            
    public event RadixSortingDelegate RadixDataCollectingEvent;
            
    public event RadixStatusDelegate RadixStatusChangedEvent;

            
    protected void RadixDataChanged(int digit, int index)
            
    {
                
    if(this.RadixDataChangedEvent != null)
                    RadixDataChangedEvent(digit, index);
            }


            
    protected void RadixDataCollecting(int digit, int index)
            
    {
                
    if(this.RadixDataCollectingEvent != null)
                    RadixDataCollectingEvent(digit, index);
            }


            
    protected void RadixStatusChanged(RadixSortStatus radixStatus)
            
    {
                
    if(this.RadixStatusChangedEvent != null)
                    RadixStatusChangedEvent(radixStatus);
            }


            
    public abstract void Sort(int[] list, Radix[] radix);
        }

    }

  10. SelectionSort
    using  System;

    namespace  Core
    {
        
    internal class SelectionSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;

                
    int loc, minIndex;

                
    for(loc = 0; loc < list.Length; loc++)
                
    {
                    minIndex 
    = minLocation(loc, list.Length - 1);
                    
    if(minIndex == -1)
                        
    return;

                    swap(loc, minIndex);
                    RedDataChanged(loc);
                }


                Finished();
            }


            
    private int minLocation(int first, int last)
            
    {
                
    int loc, minIndex;

                minIndex 
    = first;

                
    for(loc = first + 1; loc <= last; loc++)
                
    {
                    
    if(stop)
                        
    return -1;

                    GreenDataChanged(loc);
                    
    if(list[loc] < list[minIndex])
                        minIndex 
    = loc;
                }


                
    return minIndex;
            }

        }

    }

  11. ShellSort
    using  System;

    namespace  Core
    {
        
    internal class ShellSort : AbstractSort
        
    {
            
    public override void Sort(int[] list)
            
    {
                
    this.list = list;

                
    int step = list.Length/2;

                
    if(step>1)
                    ShellSwap(step);

                InsertionSort();

                Finished();
            }


            
    public void ShellSwap(int step)
            
    {
                
    forint i=0; i<list.Length - step; i++)
                
    {
                    
    if(stop)
                        
    return;

                    
    if(list[i]>list[i+step])
                    
    {
                        GreenDataChanged(i);
                        RedDataChanged(i
    +step);
                        swap(i, i
    +step);
                    }

                }


                step 
    = step/2;
                
                
    if(step == 1)
                    
    return;
                
    else
                    ShellSwap(step);

            }


            
    private void InsertionSort()
            
    {
                
    int unsortedIndex, location;
                
    int temp;

                
    for(unsortedIndex = 1; unsortedIndex < list.Length; unsortedIndex++)
                
    {
                    
    if(list[unsortedIndex] <list[unsortedIndex - 1])
                    
    {
                        temp 
    = list[unsortedIndex];
                        location 
    = unsortedIndex;

                        
    while(location > 0 && list[location - 1]>temp)
                        
    {
                            
    if(stop)
                                
    return;

                            list[location] 
    = list[location - 1];

                            
    if(location == unsortedIndex)
                                RedDataChanged(location);
                            
    else
                                GreenDataChanged(location);

                            location
    --;
                        }


                        list[location] 
    = temp;
                        DataChanged(location);
                    }

                }

            }

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值