把内容过程中比较重要的一些内容片段做个备份,如下的资料是关于Java排序算法 - 堆排序的内容。
import java.util.Arrays;
public class HeapSort {
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
public HeapSort(){
heapSort(a);
}
public void heapSort(int[] a){
System.out.println("开始排序");
int arrayLength=a.length;
for(int i=0;i<arrayLength-1;i++){
buildMaxHeap(a,arrayLength-1-i);
swap(a,0,arrayLength-1-i);
System.out.println(Arrays.toString(a));
}
}
private void swap(int[] data, int i, int j) {
int tmp=data[i];
data[i]=data[j];
data[j]=tmp;
}
private void buildMaxHeap(int[] data, int lastIndex) {
for(int i=(lastIndex-1)/2;i>=0;i--){
int k=i;
if(biggerIndex<lastIndex){
if(data[biggerIndex]<data[biggerIndex+1]){
biggerIndex++;
}
}
if(data[k]<data[biggerIndex]){
swap(data,k,biggerIndex);
k=biggerIndex;
}else{
break;
}
}
}
}
}
原文地址:https://www.cnblogs.com/gladiolus/p/11231057.html