Today look at the heap sort, didn't notice before, write a small program memory.
Heap sort is actually a completely binary tree, try to draw a tree try, the memory algorithm is very simple, big root heap satisfied with than the leaf big, small Gan on the contrary.
The idea of algorithm see Baidu Encyclopedia:
1, first the initial file R[1..N] is built into a large heap, this heap is the initial unordered area
2, then the largest record of the keyword R[1] (that is, the heap top) and the last record of the unordered zone R[n] exchange, resulting in a new unordered area r[1..n-1] and ordered area R[n], and meet R[1..n-1].keys≤r[n].key3, because of the new root after Exchange r[1] May violate the heap nature, so the current unordered area r[1..n-1] should be adjusted to a heap. Then again, R[1..n-1] The largest record of the keyword r[1] and the last record of the interval r[n-1] exchange, resulting in a new unordered area R[1..n-2] and ordered area R[N-1..N], and still satisfy the relationship r[1..n-2].keys≤r[n-1. N].keys, R[1..n-2] will also be adjusted to the heap.
Take array int[] LST = new Int[6] {4, 9, 5, 1, 7, 3};
(1) First find maximum root lst[1] = 9, swap with lst[5], result----{4, 3, 5, 1, 7,9}, unordered zone {4, 3, 5, 1, 7}, ordered area {9}
(2) The second found unordered area for {4, 3, 5, 1, 7} Max Root lst[4]=7 exchange with itself, result-----{4, 3, 5, 1, 7,9}, unordered zone {4, 3, 5, 1}, ordered area {7,9}
(3) The third found unordered area is {4, 3, 5, 1} max Root lst[2]=5 with lst[3]=1 interchange, result-----{4, 3, 1, 5, 7,9}, unordered zone {4, 3, 1}, ordered area {5,7,9}
(3) The fourth time to find the unordered zone {4, 3, 1} max Root lst[0]=4 with lst[2]=1 interchange, result----{1, 3, 4, 5, 7,9}, unordered zone {1, 3}, ordered area {4,5,7,9}
...
Time complexity O (NLOGN)
The code is as follows:
static void Main (string[] args)
{
Try
{
int[] lst = new Int[6] {4, 9, 5, 1, 7, 3};
foreach (var item in LST)
{
Console.WriteLine (Item + ",");
}
Console.WriteLine ("---------------------------");
int maxid = Getmax (lst. Length, LST);
Heapsort (Maxid, LST);
foreach (var item in LST)
{
Console.WriteLine (Item + ",");
}
Console.readkey ();
}
catch (Exception ex)
{
Console.WriteLine (ex. Message);
}
Console.readkey ();
}
<summary>
Exchange
</summary>
<param name= "Maxidx" > unordered zone Max idx</param>
<param name= "idx" > Ordered area target location </param>
<param name= "LST" > Sequence </param>
public static void Swap (int maxidx, int idx, int[] lst)
{
int midx = MAXIDX;
int temp = Lst[idx];
LST[IDX] = Lst[maxidx];
LST[MAXIDX] = temp;
}
<summary>
Get the maximum of unordered extents
</summary>
<param name= "MaxLen" > Unordered zone Maximum length </param>
<param name= "LST" > Sequence </param>
public static int Getmax (int maxlen, int[] lst)
{
int maxidx = 0;
for (var i = 0; i < maxlen; i++)
{
if (Lst[i] > Lst[maxidx])
Maxidx = i;
}
return MAXIDX;
}
<summary>
Heap Sort
</summary>
<param name= "Maxidx" > unordered area max position </param>
<param name= "LST" > Sequence </param>
<returns> sequence </returns>
public static ilist<int> heapsort (int maxidx, int[] lst)
{
int i = LST. Length-1;
int k = 0;
while (i >-1)
{
Swap (Maxidx, I, LST);
k++;
The maximum position of the disordered area is shortened each time
Maxidx = Getmax (lst. Length-k, LST);
i--;
}
return LST;
}
Heap Sort C # implementation