使用堆排序对数组进行排序
Sorting an array with heap sort
我正在做我的算法的中期审查,我试图通过 Java 实现所有的伪代码,以便更好地理解算法。但是在堆排序部分,我的代码有问题。我的输入数组是
{10,16,4,10,14,7,9,3,2,8,1}
第一个元素只代表我想要排序的元素的数量。也就是说,需要排序的元素是从索引1开始的。
我构建最大堆的输出是:
16 14 10 8 7 9 3 2 4 1
我的堆排序输出是:
1 3 2 4 7 8 9 10 14 16
我的 build-max-heap 部分似乎运行良好,但我在 heap-sort 部分也找不到错误。
public class Midterm{
public static void main(String[] args){
int[] C = {10,16,4,10,14,7,9,3,2,8,1};
/*for convenience, the first element in array C represent the
number of elements needed to be heapified;
*/
Midterm heap = new Midterm();
int n = C.length - 1;
for (int i = (n / 2); i > 0; i--){
heap.maxHeapify(C, i, n);
}
int index = 1;
while(index <= n){
System.out.print(C[index] + " ");
index++;
}
System.out.println();
Midterm heap2 = new Midterm();
heap2.heapSort(C);
int index2 = 1;
while(index2 <= n){
System.out.print(C[index2] + " ");
index2++;
}
System.out.println();
}
public void heapSort(int[] A){
int n = A.length - 1;
for (int i = n; i >= 2; i--){
exchange(A, 1, i);
maxHeapify(A, 1, i - 1);
}
}
public void maxHeapify(int[] A, int i, int n){
int left = 2 *i, right = 2 * i + 1;
int largest;
if (left < n && A[left] > A[i]){
largest = left;
}else{
largest = i;
}
if (right < n && A[right] > A[largest]){
largest = right;
}
if (largest != i){
exchange(A, i, largest);
maxHeapify(A, largest,n);
}
}
private void exchange(int[] A, int i , int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
检查下面的代码。
public class HeapSort
{
public void sort(int arr[])
{
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = arr.length;
HeapSort ob = new HeapSort();
ob.sort(arr);
System.out.println("Sorted array is");
printArray(arr);
}
}
您的代码中有 2 个错误:
1. 堆排序的 for 循环从 last element
到 first element
,所以
for (int i = n; i >= 2; i--){
应该是:
for (int i = n; i >= 1; i--){
因为索引从 0.
开始
2。执行完exchange(A, 1, i)
后,正确的调用应该是:
maxHeapify(A, 1, i)
我正在做我的算法的中期审查,我试图通过 Java 实现所有的伪代码,以便更好地理解算法。但是在堆排序部分,我的代码有问题。我的输入数组是
{10,16,4,10,14,7,9,3,2,8,1}
第一个元素只代表我想要排序的元素的数量。也就是说,需要排序的元素是从索引1开始的。
我构建最大堆的输出是: 16 14 10 8 7 9 3 2 4 1
我的堆排序输出是: 1 3 2 4 7 8 9 10 14 16
我的 build-max-heap 部分似乎运行良好,但我在 heap-sort 部分也找不到错误。
public class Midterm{
public static void main(String[] args){
int[] C = {10,16,4,10,14,7,9,3,2,8,1};
/*for convenience, the first element in array C represent the
number of elements needed to be heapified;
*/
Midterm heap = new Midterm();
int n = C.length - 1;
for (int i = (n / 2); i > 0; i--){
heap.maxHeapify(C, i, n);
}
int index = 1;
while(index <= n){
System.out.print(C[index] + " ");
index++;
}
System.out.println();
Midterm heap2 = new Midterm();
heap2.heapSort(C);
int index2 = 1;
while(index2 <= n){
System.out.print(C[index2] + " ");
index2++;
}
System.out.println();
}
public void heapSort(int[] A){
int n = A.length - 1;
for (int i = n; i >= 2; i--){
exchange(A, 1, i);
maxHeapify(A, 1, i - 1);
}
}
public void maxHeapify(int[] A, int i, int n){
int left = 2 *i, right = 2 * i + 1;
int largest;
if (left < n && A[left] > A[i]){
largest = left;
}else{
largest = i;
}
if (right < n && A[right] > A[largest]){
largest = right;
}
if (largest != i){
exchange(A, i, largest);
maxHeapify(A, largest,n);
}
}
private void exchange(int[] A, int i , int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
检查下面的代码。
public class HeapSort
{
public void sort(int arr[])
{
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = arr.length;
HeapSort ob = new HeapSort();
ob.sort(arr);
System.out.println("Sorted array is");
printArray(arr);
}
}
您的代码中有 2 个错误:
1. 堆排序的 for 循环从 last element
到 first element
,所以
for (int i = n; i >= 2; i--){
应该是:
for (int i = n; i >= 1; i--){
因为索引从 0.
开始2。执行完exchange(A, 1, i)
后,正确的调用应该是:
maxHeapify(A, 1, i)