标签:.com log 堆排序 .so sort pac heapsort stat tar
一、直接选择排序
package algorithm.sort.compare.choose;
import java.util.Arrays;
public class DirectChooseSort{
public static void main ( String[] args ){
int[] arrayA = new int[] { 11, 213, 134, 65, 77, 78, 23, 43 };
directChooseSort (arrayA);
System.out.println (Arrays.toString (arrayA));
}
private static void directChooseSort ( int[] array ){
for ( int i = 0; i < array.length; i++ ){
int index = i;
for ( int j = i + 1; j < array.length; j++ ){
if (array[index] > array[j])
index = j;
}
if (i != index){
int temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}
}
二、堆排序
package algorithm.sort.compare.choose; import java.util.Arrays; public class HeapSort { public static void main(String[] args) { int[] arrayA = new int[] { 213, 134, 65, 77, 78, 23, 43 }; method(arrayA, 0, arrayA.length); System.out.println (Arrays.toString (arrayA)); } private static void method( int[] array, int start, int len ){ int pos = ( len - 1 ) / 2; for ( int i = pos; i >= 0; i-- ){ int tmp = array[start + i]; int index = i * 2 + 1; while (index < len){ if (index + 1 < len && array[start + index] < array[start + index + 1]) // 从小到大 index += 1; if (tmp < array[start + index]){ // 从小到大 array[start + i] = array[start + index]; i = index; index = i * 2 + 1; } else break; } array[start + i] = tmp; } for ( int i = 0; i < len; i++ ){ int temp = array[start]; array[start] = array[start + len - 1 - i]; array[start + len - 1 - i] = temp; // 再一次 int post = 0; int tmp = array[start + post]; int index = 2 * post + 1; while (index < len - 1 - i){ if (index + 1 < len - 1 - i && array[start + index] < array[start + index + 1]) // 从小到大 index += 1; if (tmp < array[start + index]) {// 从小到大 array[start + post] = array[start + index]; post = index; index = post * 2 + 1; } else break; } array[start + post] = tmp; } } }
标签:.com log 堆排序 .so sort pac heapsort stat tar
原文地址:http://www.cnblogs.com/chen-yonghai/p/6367411.html