1、冒泡排序
算法描述:
时间复杂度:O(n^2)
public class BubbleSort {
static int count = 0;
static int[] shulie = new int[] {87,2,548,22,453,21,9,432,75,21,33,88};
public static void main(String[] args) {
for (int i = shulie.length; 0 < i; i--) {
for (int j = 0; j < i-1; j++) {
if (shulie[j] > shulie[j+1]) {
swap(j, j+1);
}
count++;
}
}
System.out.println(Arrays.toString(shulie));
System.out.println("比较次数:" + count);
}
static void swap(int a, int b) {
shulie[a] = shulie[a] + shulie[b];
shulie[b] = shulie[a] - shulie[b];
shulie[a] = shulie[a] - shulie[b];
}
}
2、快速排序
算法描述:
时间复杂度:O(n^2)
public class QuickSort {
static int count = 0;
static int[] shulie = new int[] {87,2,548,22,453,21,9,432,75,21,33,88};
static void swap(int a, int b) {
shulie[a] = shulie[a] + shulie[b];
shulie[b] = shulie[a] - shulie[b];
shulie[a] = shulie[a] - shulie[b];
}
public static void main(String[] args) {
kp(0, shulie.length-1);
System.out.println(Arrays.toString(shulie));
System.out.println("比较次数:" + count);
}
static void kp(int from, int to) {
boolean direct = true;
int i=from, j=to, k=shulie[from];
while (i!=j) {
if (direct) {
if (shulie[j] < k) {
swap(i, j);
direct=false;
} else {
j--;
}
} else {
if (shulie[i] > k) {
swap(i, j);
direct = true;
} else {
i++;
}
}
count++;
}
if (to-from > 1) {
if (i-1 >= from) {
kp(from,i-1);
}
if (i+1 <= to) {
kp(i+1,to);
}
}
}
}
3、堆排序
概念:
1、堆:

2、算法描述:
3、时间复杂度:O(nlgn)
public class HeapSort {
private static int[] sort = new int[] {87,2,548,22,453,21,9,432,75,21,33,88};
private static int count = 0;
public static void main(String[] args) {
buildMaxHeapify(sort);
heapSort(sort);
System.out.println(Arrays.toString(sort));
System.out.println("比较次数:" + count);
}
private static void buildMaxHeapify(int[] data) {
// 没有子节点的才需要创建最大堆,从最后一个的父节点开始
int startIndex = getParentIndex(data.length - 1);
// 从尾端开始创建最大堆,每次都是正确的堆
for (int i = startIndex; i >= 0; i--) {
maxHeapify(data, data.length, i);
}
}
/**
* 创建最大堆
*
* @param data
* @param heapSize 需要创建最大堆的大小,一般在sort的时候用到,因为最多值放在末尾,末尾就不再归入最大堆了
* @param index 当前需要创建最大堆的位置
*/
private static void maxHeapify(int[] data, int heapSize, int index) {
count++;
// 当前点与左右子节点比较
int left = getChildLeftIndex(index);
int right = getChildRightIndex(index);
int largest = index;
if (left < heapSize && data[index] < data[left]) {
largest = left;
}
if (right < heapSize && data[largest] < data[right]) {
largest = right;
}
// 得到最大值后可能需要交换,如果交换了,其子节点可能就不是最大堆了,需要重新调整
if (largest != index) {
int temp = data[index];
data[index] = data[largest];
data[largest] = temp;
maxHeapify(data, heapSize, largest);
}
}
/**
* 排序,最大值放在末尾,data虽然是最大堆,在排序后就成了递增的
*
* @paramdata
*/
private static void heapSort(int[] data) {
// 末尾与头交换,交换后调整最大堆
for (int i = data.length - 1; i > 0; i--) {
int temp = data[0];
data[0] = data[i];
data[i] = temp;
maxHeapify(data, i, 0);
}
}
/**
* 父节点位置
*
* @paramcurrent
* @return
*/
private static int getParentIndex(int current) {
return (current - 1) >> 1;
}
/**
* 左子节点position注意括号,加法优先级更高
*
* @paramcurrent
* @return
*/
private static int getChildLeftIndex(int current) {
return (current << 1) + 1;
}
/**
* 右子节点position
*
* @paramcurrent
* @return
*/
private static int getChildRightIndex(int current) {
return (current << 1) + 2;
}
/**
* 以2为底的对数
*
* @paramparam
* @return
*/
private static double getLog(double param) {
return Math.log(param) / Math.log(2);
}
}
原文:http://www.cnblogs.com/yinkh/p/6439285.html