import java.util.Arrays;
public class Sorted {
public static void main(String[] args){
Sorted sort = new Sorted();
int[] array = {1,3,7,5,19,4,9,3,34,5,23,0,0};
//sort.insertSort(array);
//sort.mergeSort(array, 0, array.length-1);
//sort.selectSort(array);
//sort.bubbleSort(array);
//sort.quickSort(array, 0, array.length-1);
sort.heapSort(array);
System.out.println(Arrays.toString(array));
}
/**
* 插入排序
* @param array
*/
public void insertSort(int[] array){
if(array==null || array.length<=0){
return;
}
for(int i=1; i<array.length; i++){
int temp = array[i];
int j = i;
while(j>0 && array[j-1]>temp){
array[j] = array[j-1];
j--;
}
array[j] = temp;
}
}
/**
* 归并排序
* @param array
* @param p
* @param r
*/
public void mergeSort(int[] array,int p, int r){
if(p<r){
int q = (p+r)/2;
mergeSort(array, p,q);
mergeSort(array, q+1, r);
merge(array, p, q, r);
}
}
private void merge(int[] array, int p,int q,int r){
int[] temp = new int[r-p+1];
int i = p;
int j = q+1;
int k = 0;
while(i<=q && j<=r){
if(array[i]<array[j]){
temp[k++]=array[i++];
}else{
temp[k++] = array[j++];
}
}
while(i<=q){
temp[k++] = array[i++];
}
while(j<=r){
temp[k++] = array[j++];
}
for(int k2=0; k2<temp.length; k2++){
array[k2+p] = temp[k2];
}
}
/**
* 快速排序
* @param array
* @param p
* @param r
*/
public void quickSort(int[] array, int p, int r){
if(p<r){
int q = partition(array,p,r);
quickSort(array, p, q-1);
quickSort(array, q+1, r);
}
}
private int partition(int[] array, int p, int r){
int base = array[r];
int i = p-1;
for(int j=p; j<r; j++){
if(array[j]<=base){
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
array[r] = array[i+1];
array[i+1] = base;
return i+1;
}
/**
* 选择排序
* @param array
*/
public void selectSort(int[] array){
if(array==null || array.length<=0){
return;
}
int index = 0;
int j = 1;
for(int i=0; i<array.length; i++){
index = 0;
j = 1;
for(; j<array.length-i; j++){
if(array[j]>array[index]){
index = j;
}
}
int temp = array[index];
array[index] = array[j-1];
array[j-1] = temp;
}
}
/**
* 冒泡排序
* @param array
*/
public void bubbleSort(int[] array){
if(array==null || array.length<=0){
return;
}
for(int i=0; i<array.length; i++){
for(int j=1; j<array.length-i; j++){
if(array[j]<array[j-1]){
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
}
/**
* 堆排序
* @param array
*/
public void heapSort(int[] array){
//构建最大堆
buildMaxHeap(array);
//将堆顶最大值放入堆尾,然后排除堆尾值,重新调整新的堆
for(int i=array.length-1; i>0; i--){
int temp = array[i];
array[i] = array[0];
array[0] = temp;
//调整堆
maxHeapify(array,0,i-1);
}
}
private void buildMaxHeap(int[] array){
for(int i= array.length/2; i>=0; i--){
maxHeapify(array,i,array.length-1);
}
}
private void maxHeapify(int[] array, int i,int heapMaxIndex){
//左孩子和右孩子
int l = 2*i;
int r = 2*i+1;
int largest = 0;
//寻找小树中的最大值
if(l<=heapMaxIndex && array[l]>array[i]){
largest = l;
}else{
largest = i;
}
if(r<=heapMaxIndex && array[r]>array[largest]){
largest = r;
}
if(largest != i){
int temp = array[i];
array[i] = array[largest];
array[largest] = temp;
maxHeapify(array,largest,heapMaxIndex);
}
}
}
排序算法
最新推荐文章于 2024-08-14 15:29:31 发布