C# selection sort | simple selection sort | principle and code implementation of heap sort

Digression: Three articles a day, cool. . .


1. What is selection sort?

  • As the name suggests, selection, selection, is to select one from the data and put it in an ordered sequence ;
  • How to choose?
  • If sorting from small to large, select , take it out, put it in the result array, and then delete the taken from the original data , and so on .

2. Simple selection sort

  • What is the idea of ​​simple selection sort?
    • is the easiest way;
    • Choose the smallest (biggest) one, take it out, and put it aside ;
    • Then select the smallest (biggest) of the remaining data , take it out, and put it aside ;
    • By repeating this step, an ordered sequence can be obtained .
  • Code
/// <summary>
/// 简单选择排序
/// </summary>
/// <param name="pArray"></param>
static public void SimpleSort(int[] pArray)
{
    
    
    int min, temp;
    //遍历每个元素,除了倒数第一个
    //因为第二层循环会从i + 1开始比较
    for (int i = 0; i < pArray.Length - 1; i++)
    {
    
    
        //找到最小值的那个索引
        min = i;
        for (int j = i + 1; j < pArray.Length; j++)
        {
    
    
            if (pArray[min] > pArray[j])
            {
    
    
                min = j;
            }
        }
        //最小值索引变了,就交换两者的位置
        if (min != i)
        {
    
    
            temp = pArray[i];
            pArray[i] = pArray[min];
            pArray[min] = temp;
        }
    }
}

  • time complexity
    • Any case: O(n^2)
    • Because no matter whether the original data is ordered or not, you still need to compare the sizes one by one.
  • Space complexity:
    • O(1)
  • Algorithm Stability:
    • unstable.

3. Heap sort

  • What is a heap?
  • for example:
  • Big root heap:
    • The value of each node in the binary tree is greater than the value of its left and right children .
    • That is to say, " the root node must be the maximum value in the whole tree ."
  • Small root pile:
    • The value of each node in the binary tree is less than the value of its left and right children .
    • That is, it is guaranteed that " the root node must be the minimum value in the entire tree ."
  • So, what is the idea of ​​​​heap sorting?
    • Build a heap (such as a big root heap);
    • Take an element from the top of the pile and put it aside, it must be the maximum value ;
    • Use the remaining elements to reconstruct a big root heap ;
    • Take another element from the top of the pile and put it aside;
    • Repeat this step and you will get an ordered sequence .
  • Code
/// <summary>
/// 堆排序
/// </summary>
/// <param name="pArray">从索引1开始有效的数组</param>
static public void HeapSort(int[] pArray)
{
    
    
    int i;
    int n = pArray.Length - 1;
    //建立一个堆,需要从最后一个非叶子结点调整
    for (i = n / 2; i >= 1; i--)
    {
    
    
        HeapAdjust2(pArray, i, n);
    }
    //将堆顶元素替换到集合尾部,并用剩余的元素重新建立一个堆
    //循环这个操作,直至只剩一个元素,一定是最小的那个
    for (i = n; i > 1; i--)
    {
    
    
        int temp = pArray[1];
        pArray[1] = pArray[i];
        pArray[i] = temp;
        HeapAdjust2(pArray, 1, i - 1);
    }
}

static public void HeapAdjust(int[] pArray, int s, int m)
{
    
    
    //记录该子树的根
    int rc = pArray[s];
    //遍历该子树的所有结点,并把最大值放在堆顶
    //为什么能保证子孩子中一定有最大值,而不是在孙子结点中?
    //因为调整是从最后一个非叶子结点调整的,也就是说,当子树
    //深度大于2时,其根节点的孩子结点,一定是子孙节点中最大的两个值。
    for (int j = 2 * s; j <= m; j *= 2)
    {
    
    
        //从两个孩子中取最大的那个
        if (j < m && pArray[j] < pArray[j + 1])
        {
    
    
            ++j;
        }
        //根比最大的孩子还要大,就不用找了
        if (rc >= pArray[j])
        {
    
    
            break;
        }
        //把孩子的值给到根节点
        pArray[s] = pArray[j];
        s = j;
    }
    //把根节点的值给到替换的孩子
    pArray[s] = rc;
}

  • time complexity
    • O(nlogn)
  • Space complexity:
    • O(1)
  • Algorithm Stability:
    • unstable.


Conclusion:
The code for heap sorting still needs to be written silently by yourself to understand its meaning.
The most important of these is the "heap adjustment" function.
This function can both build a heap and adjust a heap.
To put it bluntly, building a heap = starting from the last non-leaf node, forward, "heap adjustment" node by node.

Guess you like

Origin blog.csdn.net/Liyager/article/details/129209712