public class HeapSort
{
public static void sort(int[] a)
{
int N = a.length;
for (int k = N/2; k >= 1; k--)
sink(a, k, N);
while (N > 1)
{
exch(a, 1, N--);
sink(a, 1, N);
}
}
private void sink(int[] arr, int k, int N)
{
while(2*k <= N)
{
int j = 2*k;
if (j < N && arr[j - 1] < arr[j])) j++;
if (arr[k - 1] >= arr[j - 1]) break;
k = j;
}
}
private void exch(int[] arr, int i, int j)
{
int tmp = arr[?];
arr[j - 1] = ?;
arr[?] = tmp;
}
}
code test2
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment