我正在尝试在java中实现堆排序。我在互联网上搜索了一些伪代码,然后我查看了这三个网站:
http://www.cc.gatech.edu/classes/cs3158_98_fall/heapsort.html
http://www.algorithmist.com/index.php/Heap_sort
http://thevigilantzephyr.blogspot.ie/2011/11/heap-sort-algorithm-and-pseudo-code.html
现在我已经在java中实现了一个解决方案,但是当我运行它时,似乎并没有对数组的第一个元素和最后一个元素进行排序。
这是我的代码:
package heapSort;
import java.util.Comparator;
public class heapSort
{
static int heapSize;
public static <type> void sort(type[] array, Comparator<type> comp)
{
buildHeap(array, comp);
for (int index = array.length - 1; index > 1; index--)
{
swap(array, 1, index);
heapSize--;
heap(array, 1, comp);
}
}
private static <type> void buildHeap(type[] array, Comparator<type> comp)
{
heapSize = array.length - 1;
for (int index = (int) Math.floor((array.length - 1) / 2); index > 1; index--)
{
heap(array, index, comp);
}
return;
}
private static <type> void heap(type[] array, int index, Comparator<type> comp)
{
int left = 2 * index;
int right = 2 * index + 1;
int largest;
if (left <= heapSize && comp.compare(array[left], array[index]) > 0)
{
largest = left;
}
else
{
largest = index;
}
if (right <= heapSize && comp.compare(array[right], array[largest]) > 0)
{
largest = right;
}
if (largest != index)
{
swap(array, index, largest);
heap(array, largest, comp);
}
}
private static <type> void swap(type[] array, int index1, int index2)
{
type temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
return;
}
}
这是我的测试人员类:
package heapSort;
import java.util.Arrays;
import java.util.Comparator;
public class quicksortTesterinplcae
{
public static void main(String[] args)
{
/**
* String comparator alphabetical
*/
Comparator<String> comp = new Comparator<String>()
{
public int compare(String arg0, String arg1)
{
return arg0.compareTo(arg1);
}
};
/**
* Integer comparator ascending
*/
Comparator<Integer> compint = new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return o1.compareTo(o2);
}
};
/**
* Two test data sets
*/
String[] test = "hello world the cat sat on the bloody mat".split("\\s");
Integer[] testint =
{ 4, 2, 3, 4, 8, 9, 1, 2, 3, 7, 9, 8, 4, 2, 33, 22, 44, 66, 77, 88, 9, 87, 5, 3, 22 };
/**
* Print the unsorted data
*/
System.out.println(Arrays.toString(test));
System.out.println(Arrays.toString(testint));
/**
* Sort the two data sets
*/
heapSort.sort(test, comp);
heapSort.sort(testint, compint);
/**
* Print the sorted sets
*/
System.out.println(Arrays.toString(test));
System.out.println(Arrays.toString(testint));
System.exit(0);
}
}
但是当我运行代码时,我得到以下内容:
[hello,world,the,cat,sat,on,the,bloody,mat]
[4,2,3,4,8,9,1,2,3,7,9,8,4,2,33,22,44,66,77,88,9,87,5, 3,22]
[hello,bloody,cat,mat,on,sat,the,the,world]
[4,1,2,3,3,3,4,4,5,7,8,9,9,9,9,22,22,33,44,66,77,87, 88,2]
答案 0 :(得分:0)
您可以用于堆排序的代码可以是: -
class heapSort {
static int heapSize;
public static <type> void sort(type[] arr, Comparator<type> comp) {
heapSize = arr.length;
// Build heap (rearrange array)
for (int i = heapSize / 2 - 1; i >= 0; i--)
heap(arr,heapSize, i, comp);
// One by one extract an element from heap
for (int i = heapSize - 1; i >= 0; i--) {
// Move current root to end
type temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heap(arr, i,0, comp);
}
}
private static <type> void heap(type[] arr,int n, int i, Comparator<type> comp) {
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 && comp.compare(arr[l], arr[largest])>0)
largest = l;
// If right child is larger than largest so far
if (r < n && comp.compare(arr[r], arr[largest])>0)
largest = r;
// If largest is not root
if (largest != i) {
type swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heap(arr,n, largest, comp);
}
}
private static <type> void swap(type[] array, int index1, int index2) {
type temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
return;
}
}