I am trying to implement heap-sort in java. I searched the internet for some Pseudo-code and I looked at these three sites:
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
Now I have implemented a solution in java but when I run it it does not seem to sort the first element and the last element of the array.
Here is my code:
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;
}
}
Here is my tester class:
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);
}
}
But when I run the code I get the following:
[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, 2, 3, 3, 3, 4, 4, 5, 7, 8, 8, 9, 9, 9, 22, 22, 33, 44, 66, 77, 87, 88, 2]
The code you can use for Heap sort could be:-
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.