ソート、ツリーソート、スタックソートのjavaコード実装の選択
package com.sort;
/**
* :
* ,
*
*/
public class SelecSortDemo {
/**
* --------------------------------------------
*
* : n , ,
* ,
* ,
* , , 。
*/
public static void simpleSelectSort(Object[] a){
int len = a.length;
for(int i = 0,j;i<len;i++){
j = selectMin(a, i);
if(i!=j) //
a[i] = a[j];
}
}
/**
*
* i
*
*/
private static int selectMin(Object[] a,int low){
int min = low; //
for(int i = low+1;i<a.length;i++){
if(((Comparable)a[i]).compareTo(a[min])<0){
min = i;
}
}
return min;
}
/**
* ---------------------------------------
* :
* , n-1 , n-2 ,
* ( ), 。
* , ,
* , ,
* 。
*
* :
* , n , n/2 ,
* , , , 。
* , 2n-1。 ,
* :49 38 65 97 76 13 27 49
* : 13
* 38 13
* 38 65 13 27
* 19 38 65 97 76 13 27 49
* 13 ,
*
* , , a ,
* ,......
*
* , , , ,
* , , , 。
* ,
* , ( ) 。
* 76 27 , 27 38 27。
* , 。
*
* PS: ,
* , Integer.MAX_VALUE
*/
public static void treeSelectSort(Object[] a){
int len = a.length;
int treeSize = 2 * len - 1; //
int low = 0;
Object[] tree = new Object[treeSize]; //
// , 0
for(int i = len-1,j=0 ;i >= 0; --i,j++){ //
tree[treeSize-1-j] = a[i];
}
for(int i = treeSize-1;i>0;i-=2){ //
tree[(i-1)/2] = ((Comparable)tree[i-1]).compareTo(tree[i]) < 0 ? tree[i-1]:tree[i];
}
//
int minIndex;
while(low < len){
Object min = tree[0]; //
a[low++] = min;
minIndex = treeSize-1;
//
while(((Comparable)tree[minIndex]).compareTo(min)!=0){
minIndex--;
}
tree[minIndex] = Integer.MAX_VALUE; //
//
while(minIndex > 0){ //
if(minIndex % 2 == 0){ //
tree[(minIndex-1)/2] = ((Comparable)tree[minIndex-1]).compareTo(tree[minIndex])
< 0 ? tree[minIndex-1]:tree[minIndex];
minIndex = (minIndex-1)/2;
}else{ //
tree[minIndex/2] = ((Comparable)tree[minIndex]).compareTo(tree[minIndex+1])
< 0 ? tree[minIndex]:tree[minIndex+1];
minIndex = minIndex/2;
}
}
}
}
/**
* ----------------------------------
*
*
* , 。
* : 1 n/2 , k(i)<=k(2i),k(i)<=k(2i+1)
* k(i)>=k(2i),k(i)>=k(2i+1)
* : ,
* , n/2( n/2) i ,
* k(i)<=k(2i),k(i)<=k(2i+1)。
*
* :
* ,
* 1. : n/2, ,
* 。 n/2-1 ,
* , , ....,
* :49 38 65 97 76 13 27 49
* :
* 49
* 38 65
* 97 76 13 27
* 49
*
* 13
* 38 27
* 49 76 65 49
* 97
* :97<——>49, 13<--->65,38 ,49<-->13,13<-->27
* 2.
* , ,
* 。 13 97 , 97 27 ,
* 97 49 , 27, 27 ,
* ,
*/
public static void heapSort(Object[] a){
int len = a.length;
//
for(int i=(len-1)/2;i>=0;i--){
heapAdjust(a,i,len);
}
//
int count = len-1;
while(count > 0 ){
//
swap(a,0,count);
count -- ;
heapAdjust(a,0,count);
}
}
/**
* ,
*
*/
private static void heapAdjust(Object[] a,int i,int len){
Object parent = a[i];
for(int j = (i+1) * 2 - 1;j < len; j = (j+1) * 2 - 1){ //
if(j < len-1 && ((Comparable)a[j]).compareTo(a[j+1]) < 0 ){
++j; //
}
if(((Comparable)parent).compareTo(a[j]) > 0) //
break;
a[i] = a[j];
i = j ;
}
a[i] = parent; //parent
}
/**
*
*/
private static void swap(Object[] a,int i,int j){
Object temp = null;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
//just for test
public static void main(String[] args) {
Integer[] data = {49,38,65,97,76,13,27,49};
SelecSortDemo.treeSelectSort(data);
for(Integer d:data){
System.out.println(d);
}
}
}
転載先:http://zhouyunan2010.iteye.com/blog/1217462