• 排序算法


    //选择排序
    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
    }
    int t = arr[min]
    arr[min] = arr[i]
    arr[i] = t
    }
    }
    static void main(string[] args)
    {
    int[] array = new int[] { 1 5 3 6 10 55 9 2 87 12 34 75 33 47 }
    selectionsorter s = new selectionsorter()
    s.sort(array)
    foreach (int m in array)
    console.writeline(" {0}" m)
    }
    }
    //冒泡排序
    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] //交换数据
    arr[i + 1] = temp
    }
    }
    j++
    }
    }

    static void main(string[] args)
    {
    int[] array = new int[] { 1 5 3 6 10 55 9 2 87 12 34 75 33 47 }
    ebullitionsorter e = new ebullitionsorter ()
    e.sort(array)
    foreach (int m in array)
    console.writeline(" {0}" m)

    }
    }
    //快速排序
    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 //存储分支点
    int l 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)
    }

    static void main(string[] args)
    {
    int[] iarrary = new int[] { 1 5 3 6 10 55 9 2 87 12 34 75 33 47 }
    quicksorter q = new quicksorter()
    q.sort(iarrary 0 13)
    for (int m = 0 m < = 13 m++)
    console.writeline(" {0}" iarrary[m])
    }
    }
    //插入排序
    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] //交换顺序
    --j
    }
    arr[j] = t
    }
    }
    static void main(string[] args)
    {
    int[] array = new int[] { 1 5 3 6 10 55 9 2 87 12 34 75 33 47 }
    insertionsorter i = new insertionsorter()
    i.sort(array)
    foreach (int m in array)
    console.writeline(" {0}" m)
    }
    }
    //希尔排序
    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] //交换数据
    j -= inc
    }
    arr[j - 1] = t
    }
    }
    }

    static void main(string[] args)
    {
    int[] array = new int[] { 1 5 3 6 10 55 9 2 87 12 34 75 33 47 }
    shellsorter s = new shellsorter()
    s.sort(array)
    foreach (int m in array)
    console.writeline(" {0}" m)

    }
    }

  • 相关阅读:
    【BZOJ4945&&UOJ317】游戏(2-sat,拓扑序)
    【Hihocoder1636】Pangu and Stones(区间DP)
    【BZOJ1579】Revamping Trails(分层图,最短路,堆)
    【NOIP2017】逛公园(最短路图,拓扑排序,计数DP)
    【HDOJ6218】Bridge(线段树,set,网格图,连通性)
    【BZOJ1018】堵塞的交通traffic(线段树,网格图,连通性)
    【HDOJ6217】BBP Formula(公式)
    【HDOJ6224】Legends of the Three Kingdoms(概率DP)
    152.图论
    151.函数
  • 原文地址:https://www.cnblogs.com/ahao214/p/3413824.html
Copyright © 2020-2023  润新知