[英]Sorting an array with heap sort
I was working my Algorithm's midterm review and I tried to implement all the pseudo codes by Java in order to have a better understanding of algorithm. 我正在进行算法的中期审查,并尝试用Java实现所有伪代码,以便更好地理解算法。 But on the heap sort part, there was something wrong with my code. 但是在堆排序部分,我的代码出了问题。 My input array is 我的输入数组是
{10,16,4,10,14,7,9,3,2,8,1} {} 10,16,4,10,14,7,9,3,2,8,1
and the first element just represents the number of elements that I would like to sort. 第一个元素只表示我想要排序的元素数量。 In other words, the elements needed to be sorted start from index 1. 换句话说,需要排序的元素从索引1开始。
My output of build max-heap is : 16 14 10 8 7 9 3 2 4 1 我的构建最大堆的输出是:16 14 10 8 7 9 3 2 4 1
And my output of heap sort is : 1 3 2 4 7 8 9 10 14 16 我的堆排序输出是:1 3 2 4 7 8 9 10 14 16
It seemed my build-max-heap part worked well but I couldn't find bugs in heap-sort part, either. 似乎我的build-max-heap部分运行良好,但我也找不到堆排序部分中的错误。
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;
}
}
check the below code. 检查以下代码。
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);
}
}
There are 2 mistakes in your code: 您的代码中有2个错误:
1. The for loop for the heapsort goes from last element
to first element
, so 1. heapsort的for循环从last element
到first element
,所以
for (int i = n; i >= 2; i--){
should be: 应该:
for (int i = n; i >= 1; i--){
because the indices start from 0 . 因为指数从0开始。
2 . 2 。 After performing a exchange(A, 1, i)
, the right call should be: 在执行exchange(A, 1, i)
,正确的呼叫应该是:
maxHeapify(A, 1, i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.