Java 堆排序超过了我
我正在尝试实现一个基于数组的堆排序,它对前几个堆进行排序,但不完全,我不知道为什么。以下是我的工作内容:Java 堆排序超过了我,java,heapsort,Java,Heapsort,我正在尝试实现一个基于数组的堆排序,它对前几个堆进行排序,但不完全,我不知道为什么。以下是我的工作内容: public class HeapSort{ static int[] numbers = new int[] { 0, 13, 8, 5, 10, 9, 15, 1, 2, 3, 6, 4, 12, 14, 7, 11 }; static int[] array = new int[16]; public static void main(String[] ar
public class HeapSort{
static int[] numbers = new int[] { 0, 13, 8, 5, 10, 9, 15, 1, 2, 3, 6, 4, 12, 14, 7, 11 };
static int[] array = new int[16];
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
array[i] = numbers[i];
if (i > 1)
sort(i);
}
for (int i = 1; i < 15; i++) {
System.out.println(array[i]);
}
}
public static void sort(int i) {
int parentLocation = i / 2;
int childLocation = i;
int parentValue = array[parentLocation];
int childValue = array[childLocation];
if (childValue < parentValue) {
array[parentLocation] = childValue;
array[childLocation] = parentValue;
sort(parentLocation);
}
}
}
公共类HeapSort{
静态int[]数=新int[]{0,13,8,5,10,9,15,1,2,3,6,4,12,14,7,11};
静态int[]数组=新int[16];
公共静态void main(字符串[]args){
对于(int i=0;i<15;i++){
数组[i]=数字[i];
如果(i>1)
类别(i);
}
对于(int i=1;i<15;i++){
System.out.println(数组[i]);
}
}
公共静态无效排序(int i){
int parentLocation=i/2;
int childLocation=i;
int parentValue=数组[parentLocation];
int childValue=数组[childLocation];
if(childValue
我确信这是我使用递归调用父级排序的错误,但我不明白为什么
TIA我用c#写了这个,很容易阅读。你的impl缺失了很多。比如希皮菲。看一看。希望有帮助。这是使用heapify和max heap概念的heap\u sort。我按照“算法简介”一书中的算法编码。看一看。
This is heap_sort which uses heapify and max heap concepts. I coded as per the algorithm in "Intro to Algos" book. Have a look.
#include "stdafx.h"
#define LEFT(i) (2 * (i))
#define RIGHT(i) (((2 * (i)) + 1))
#define PARENT(i) ((i) / 2))
void print_heap(int input[], int n)
{
int i;
printf("Printing heap: \n");
for (i = 0; i < n; i++)
printf("%d ", input[i]);
printf("\n");
}
void swap_nodes(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void max_heapify(int input[], int i, int n)
{
int largest;
int l = LEFT(i + 1) - 1; // Get 0 based array index
int r = RIGHT(i + 1) - 1; // Get 0 based array index
if (l < n && input[l] > input[i]) {
largest = l;
} else {
largest = i;
}
if (r < n && input[r] > input[largest]) {
largest = r;
}
if (largest != i) {
swap_nodes(&input[i], &input[largest]);
max_heapify(input, largest, n);
}
}
void heapify(int input[], int n)
{
for (int i = n/2; i >= 0; i--)
max_heapify(input, i, n);
}
void heap_sort(int input[], int n)
{
heapify(input, n);
for (int i = n - 1; i >= 1; i--) {
swap_nodes(&input[0], &input[i]);
n = n - 1;
max_heapify(input, 0, n);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//int input[] = {6, 5, 3, 1, 8, 7, 4, 2};
//int input[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
int input[] = {5, 3, 17, 10, 84, 19, 6, 22, 9, 1};
//int input[] = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1};
int n = sizeof(input) / sizeof(input[0]);
print_heap(input, n);
heap_sort(input, n);
print_heap(input, n);
return 0;
}
#包括“stdafx.h”
#定义左(i)(2*(i))
#定义权利(i)((2*(i))+1))
#定义父项(i)((i)/2))
无效打印堆(int输入[],int n)
{
int i;
printf(“打印堆:\n”);
对于(i=0;iinput[i]){
最大=l;
}否则{
最大=i;
}
如果(rinput[maximust]){
最大=r;
}
如果(最大!=i){
交换_节点(&input[i],&input[max]);
最大值(输入,最大值,n);
}
}
void heapify(int输入[],int n)
{
对于(int i=n/2;i>=0;i--)
max_heapify(输入,i,n);
}
无效堆排序(int输入[],int n)
{
heapify(输入,n);
对于(int i=n-1;i>=1;i--){
交换_节点(&input[0],&input[i]);
n=n-1;
最大值(输入,0,n);
}
}
int _tmain(int argc,_TCHAR*argv[]
{
//int输入[]={6,5,3,1,8,7,4,2};
//int输入[]={4,1,3,2,16,9,10,14,8,7};
int输入[]={5,3,17,10,84,19,6,22,9,1};
//int输入[]={16,14,10,8,7,9,3,2,4,1};
int n=sizeof(输入)/sizeof(输入[0]);
打印堆(输入,n);
堆排序(输入,n);
打印堆(输入,n);
返回0;
}
答案的内容应多于链接。