菜鸟学排序算法

插入排序:

public List<int> InsertSort(List<int> arr)
{
    List<int> ARRAY = new List<int>();
    ARRAY.Add(arr[0]);
    int value;
    int count;

    for (int arrIndex = 1; arrIndex < arr.Count; arrIndex++)
    {
        value = arr[arrIndex];
        count = ARRAY.Count;

        for (int ArrayIndex = 0; ArrayIndex < count; ArrayIndex++)
        {
            if (value <= ARRAY[ArrayIndex])
            {
                ARRAY.Insert(ArrayIndex, value);
                break;
            }
        }
        if (count >= ARRAY.Count)
            ARRAY.Add(value);
    }
    return ARRAY;
}

希尔排序:

private List<int> ShellSort(List<int> arr)
{
    List<int> ARRAY = new List<int>();
    int delta = arr.Count;
    ARRAY = arr;
    do
    {
        delta = (delta / 3) + 1;
        for (int i = 0; i < delta; i++)
        {
            List<int> list = new List<int>();
            for (int j = 0; j < arr.Count / delta; j++)
            {
                list.Add(arr[j * delta + i]);
            }
            list = InsertSort(list);
            for (int j = 0; j < arr.Count / delta; j++)
            {
                arr[j * delta + i] = list[j];
            }
        }
    }
    while (delta > 1);

    return ARRAY;
}

快速排序:

private List<int> QuickSort(List<int> arr, int first, int last)
{
    if (first >= last)
        return null;

    int First = first;
    int Last = last;
    int key = array[first];

    while (first < last)
    {
        while (array[last] >= key && last > first)
            --last;

        array[first] = array[last];
        while (array[first] <= key && last > first)
            ++first;

        array[last] = array[first];
    }
    array[first] = key;

    QuickSort(arr, First, first - 1);
    QuickSort(arr, last + 1, Last);
    return arr;
}

归并排序:

private List<int> MergeSort(List<int> arr)
{
    List<int> ARRAY = new List<int>();
    int delta = 1;
    int i, j;

    while (delta < arr.Count)
    {
        ARRAY.Clear();
        while (arr.Count > 0)
        {
            i = 0; j = i + delta;

            while (i < delta && j < 2 * delta && j < arr.Count)
            {
                if (arr[i] < arr[j])
                {
                    ARRAY.Add(arr[i]);
                    i++;
                }
                else
                {
                    ARRAY.Add(arr[j]);
                    j++;
                }
            }
            while (i < delta && i < arr.Count)
            {
                ARRAY.Add(arr[i]);
                i++;
            }
            while (j < 2 * delta && j < arr.Count)
            {
                ARRAY.Add(arr[j]);
                j++;
            }

            arr.RemoveRange(0, Math.Min(j, arr.Count));
        }
        delta *= 2;
        arr = new List<int>(ARRAY);
    }

    return ARRAY;
}

基数排序:

private List<int> RadixSort(List<int> arr)
{
    List<int> ARRAY = new List<int>();
    List<int>[] ARRAYS = new List<int>[10];
    int count = 0;int index = 0;
    int Max = arr.Max().ToString().Length;

    while (count < Max)
    {
        ARRAY.Clear();
        for (int i = 0; i < ARRAYS.Length; i++)
            ARRAYS[i] = new List<int>();
        foreach (int Item in arr)
        {
            index = (Item / (int)Math.Pow(10, count)) % 10;
            {
                ARRAYS[index].Add(Item);
            }
        }

        for (int i = 0; i < 10; i++)
        {
            ARRAY.AddRange(ARRAYS[i]);
        }

        arr = new List<int>(ARRAY);
        count++;
    }

    return ARRAY;
}

二叉树定义:

class Tree
{
    public int value;
    public Tree left = null;
    public Tree right = null;

    public Tree(int key)
    {
        value = key;
    }
}

二叉排序树:

public List<int> BinaryTreeSort(List<int> arr)
{
    Tree tree = null;
    AddListToBinaryTree(ref tree, arr);
    arr.Clear();
    arr = LoadListFromTree(tree, ref arr);

    return arr;
}

private void AddListToBinaryTree(ref Tree tree, List<int> list)
{
    foreach (int key in list)
    {
        AddKeyToBinaryTree(ref tree, key);
    }
}

private void AddKeyToBinaryTree(ref Tree tree, int key)
{
    if (tree == null)
    {
        tree = new Tree(key);
    }
    else if (  key < tree.value)
    {
        AddKeyToBinaryTree(ref tree.left, key);
    }
    else if (tree.value < key)
    {
        AddKeyToBinaryTree(ref tree.right, key);
    }
}

private List<int> LoadListFromTree(Tree tree, ref List<int> list)
{
    if (tree == null)
        return null;

    LeftLoadListFromTree(tree.left, ref list);
    list.Add(tree.value);
    LeftLoadListFromTree(tree.right, ref list);

    return list;
}

堆排序(求最大值):

private int HeapSort(List<int> arr)
{
    Tree tree = null;
    AddListToHeapSort(ref tree, arr);
    return tree.value;
}
private void AddListToHeapSort(ref Tree tree, List<int> list)
{
    foreach (int key in list)
    {
        AddKeyToHeapSort(ref tree, key);
    }
}
private void AddKeyToHeapSort(ref Tree tree, int key)
{
    if (tree == null)
    {
        tree = new Tree(key);
    }
    else if (tree.value >= key)
    {
        if (tree.left == null)
            AddKeyToHeapSort(ref tree.left, key);
        else if (tree.right == null)
            AddKeyToHeapSort(ref tree.right, key);
        else
        {
            if (tree.left.value > tree.right.value)
            {
                AddKeyToHeapSort(ref tree.left, key);
            }
            else
                AddKeyToHeapSort(ref tree.right, key);
        }
    }
    else
    {
        AddKeyToHeapSort(ref tree, tree.value);
        tree.value = key;
    }
}

写的很烂。。。
权当每天的日记吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值