HEAP SORT

xiaoxiao2021-04-10  608

OK.

1, [N / 2] -1 nodes;

2, Others Are Not Nodes;

3, Time <= 2nlog2 (n)

// Get the max hEAP

Void Sift (INT * A, INT N)

{

INT I, J, K;

For (i = (n / 2-1); i> = 0; I -)

{

K = I;

J = 2 * k 1;

INT TEMP = * (A K);

INT FLAG = 0;

While (Flag == 0 && J

{

IF (* (A J) <* (A J 1))

J ;

IF (Temp <* (A J))

{

* (A K) = * (a j);

K = J;

}

Else

FLAG = 1;

* (a k) = TEMP;

}

}

}

Void Heapsort (int * a, int N, int out [])

{

INT K;

INT I, J;

J = 0;

Int temp;

SIFT (A, N);

For (k = 0; k

Printf ("% d", a [k]);

Printf ("/ n");

For (i = n-1; i> 1; I -)

{

OUT [J] = * A;

J ;

// SWAP

Temp = * a;

* a = * (A I);

* (A I) = TEMP;

SIFT (A, I-1);

}

OUT [J] = a [0]> a [1]? a [0]: a [1];

OUT [J 1] = a [0]

}


New Post(0)