您的位置:首页 > 编程语言 > Java开发

排序算法集合(java版):网上收集未测试

2009-07-13 21:17 405 查看
package test;

public class SortUtil {

/**
* 冒泡排序 Bubble Sort
* <p>
* 原理: 比较n轮,每一轮都把最大元素移动到数组后端。
*
* @return
*/
public int[] bubbleSort(int[] result) {
int ARRAYSIZE = result.length;
for (int i = 0; i < ARRAYSIZE; i++) {
for (int j = i + 1; j < ARRAYSIZE; j++) {
if (result[i] > result[j]) {
// 交换
swap(result, i, j);
}
}
}

return result;
}

/**
* 插入排序 Insert Sort
* <p>
* 原理: 从第二个元素开始,因为左侧的数组为排序后的数组, 只要将当前元素插入到左侧数组的适当位置,就能保持数组为有序
* 然后处理第三个元素...直到最后一个元素
*
* @return
*/
public int[] insertSort(int[] result) {
int ARRAYSIZE = result.length;
for (int i = 1; i < ARRAYSIZE; i++) {
for (int j = i; j > 0 && result[j] < result[j - 1]; j--) {
swap(result, j, j - 1);

}
}

return result;
}

/**
* 折半搜索插入排序 BinarySearchThenInsert Sort
* <p>
* 原理与插入排序类似,不同点在于寻找插入位置的时候,采取的是折半查找方法
*
* @return
*/
public int[] binsertSort(int[] result) {
int ARRAYSIZE = result.length;
for (int i = 1; i < ARRAYSIZE; i++) {
if (result[i] < result[0]) {
int temp = result[i];
for (int j = i - 1; j >= 0; j--) {
result[j + 1] = result[j];

}
result[0] = temp;

} else if (result[i] < result[i - 1]) {
int larrange = 0;
int rarrange = i - 1;
while (rarrange - larrange > 1) {
int p = (rarrange + larrange + 1) / 2;
if (result[i] < result[p]) {
rarrange = p;
} else {
larrange = p;
}

}
int temp = result[i];
for (int j = i - 1; j >= larrange + 1; j--) {
result[j + 1] = result[j];

}
result[larrange + 1] = temp;

}
}

return result;
}

/**
* 堆排序 Heap Sort
* <p>
* 原理: 利用了堆的易调整的特点来进行的一种选择排序。 以大顶堆为例,什么是大顶堆?
* 大顶堆的逻辑结构是一颗完全二叉树,[把满二叉树最后一层右侧的一些叶子摘掉] 假设其高度为h,则元素个数介于 1 + 2 + ... +
* exp(2, h - 2) ~ 1 + 2 + ... + exp(2, h -1)之间 符合如下定义为大顶堆:(此定义基于大顶堆的顺序存储结构)
* for (int i = array.length - 1; i > 0; i --) { 任意 array[i] <= array[(i -
* 1)/2]; } (还有一种是小顶堆,不同的只是比较时候的大于号方向不同)。 容易想到,当堆顶元素(MaxValue)被替换后,
* 至多只要在双亲和子节点间进行h(大顶堆的高度) - 1次交换, (参照交换算法可以发现比较次数一般来说是交换次数的2~3倍,也不算多)
* 就可以形成新的大顶堆。由此大大提高了排序效率。
*
* @return
*/
public int[] heapSort(int[] result) {

// 初始化无序数组为大顶堆
for (int i = result.length - 2; i >= 0; i--) {
adjustHeap(result, i, result.length - 1);
}

// 将最大值元素交换至数组末端,并调整前端为大顶堆,循环直至前端只剩下一个元素
for (int i = result.length - 1; i > 0; i--) {
swap(result, 0, i);
adjustHeap(result, 0, i - 1);
}

return result;
}

/**
* 将除顶(不确定是否满足大顶堆条件)外,左子树和右子树都为一个堆的数组调整为大顶堆
*
* @param array
*            待调整数组
* @param from
*            顶的指针
* @param to
*            调整的末端(就是调整array[from]...array[to]这一段为一个大顶堆)
*/
private void adjustHeap(int[] array, int from, int to) {
int i = 0;
// 比较节省比较次数的方法,只要比较到比其左右子树的根结点的值都大,就可以return了
while (from + 2 * i + 2 <= to) {
if (array[from + i] < array[from + 2 * i + 1]
|| array[from + i] < array[from + 2 * i + 2]) {
if (array[from + 2 * i + 1] > array[from + 2 * i + 2]) {
swap(array, from + i, from + 2 * i + 1);
i += i + 1;
} else {
swap(array, from + i, from + 2 * i + 2);
i += i + 2;
}

} else {
return;
}
}
if (from + 2 * i + 1 == to && array[from + i] < array[from + 2 * i + 1]) {
// 有时会出现仅存在左子树的情况(左子树为调整数组的最后一个元素)
swap(array, from + i, from + 2 * i + 1);

}
}

/**
* 快速排序 Quick Sort
* <p>
* 原理: 选择数组中的一个元素作为标准,将所有比标准小的元素放到左边, 所有比标准大的元素放到右边。 并对左边和右边的元素做一样的快速排序过程。
*
* @return
*/
public int[] quickSort(int[] result) {
quick(result, 0, result.length - 1);
return result;
}

/**
* 选择数组中的一个元素作为标准,将所有比标准小的元素放到左边, 所有比标准大的元素放到右边。 并对左边和右边的元素做一样的快速排序过程。
*
* @param array
* @param startIndex
* @param endIndex
*/
private void quick(int[] array, int startIndex, int endIndex) {
int pIndex = startIndex;
for (int i = startIndex + 1; i <= endIndex; i++) {
if (array[i] < array[pIndex]) {
int temp = array[i];
for (int j = i; j > pIndex; j--) {
array[j] = array[j - 1];
}
array[pIndex] = temp;
pIndex++;
}

}
if (pIndex - startIndex > 1) {
quick(array, startIndex, pIndex - 1);
}
if (endIndex - pIndex > 1) {
quick(array, pIndex + 1, endIndex);
}
}

/**
* 归并排序 Merge Sort
* <p>
* 原理: 分治。将数组分为左,右两部分, 首先将数组分为左右两部分,分别进行归并排序, 然后合并左右两部分的排序结果就构成了一个有序数组。
*
* @return
*/
public int[] mergeSort(int[] result) {

mergeR(result, 0, result.length - 1);

return result;
}

/**
* 递归对数组进行归并排序
*
* @param array
* @param startIndex
* @param endIndex
*/
private void mergeR(int[] array, int startIndex, int endIndex) {
if (startIndex < endIndex) {
int mid = (startIndex + endIndex) / 2;
// 对包括中点在内的左侧数组区间进行归并排序
mergeR(array, startIndex, mid);
// 对中点之后的右侧数组区间进行归并排序
mergeR(array, mid + 1, endIndex);
// 合并左和右两个独立的有序区间为一个有序区间
merge(array, startIndex, mid, endIndex);
}
}

/**
* 将array数组的两个有序区间array[startIndex]...array[midIndex] 和array[midIndex +
* 1]...array[endIndex]合并为一个有序区间 array[startIndex]...array[endIndex]
*
* @param array
* @param startIndex
* @param midIndex
* @param endIndex
*/
private void merge(int[] array, int startIndex, int midIndex, int endIndex) {
int[] resultTemp = new int[endIndex - startIndex + 1];

int pr = 0;
int p1 = startIndex;
int p2 = midIndex + 1;
while (p1 <= midIndex || p2 <= endIndex) {
if (p1 == midIndex + 1) {
while (p2 <= endIndex) {
resultTemp[pr++] = array[p2++];

}
} else if (p2 == endIndex + 1) {
while (p1 <= midIndex) {
resultTemp[pr++] = array[p1++];

}
} else if (array[p1] <= array[p2]) {
resultTemp[pr++] = array[p1++];

} else {
resultTemp[pr++] = array[p2++];

}
}
for (p1 = startIndex, p2 = 0; p1 <= endIndex; p1++, p2++) {
array[p1] = resultTemp[p2];

}
}

/**
* 希尔排序 Shell Sort
* <p>
* 原理: 分别以数组大小的1/2,1/4,1/8....1的作为步伐d, 将array[i],array[i + d],array[i +
* 2d]....array[i + nd]看作一个数组进行排序, 与插入排序相比,因为可以更有效的消除逆序,因此交换次数是很少的,
* 缺点是比较次数过多
*
* @return
*/
public int[] shellSort(int[] result) {
int ARRAYSIZE = result.length;
for (int d = ARRAYSIZE / 2; d > 0; d = d / 2) {
// print(result);
for (int i = d; i < ARRAYSIZE; i++) {
for (int j = i; j >= d; j = j - d) {
if (result[j] < result[j - d]) {
swap(result, j, j - d);
}

}
}
}

return result;
}

/**
* 简单选择排序 SimpleSelection Sort
* <p>
* 原理:每遍历未排序部分一次都选出一个最小值,并将最小值元素移动到数组前端
*
* @return
*/
public int[] simpleSelectionSort(int[] result) {

// 重复此过程:选取最小值,并将其交换至数组前端
int minIndex = 0;
for (int i = 0; i < result.length; i++) {
minIndex = i;
for (int j = i + 1; j < result.length; j++) {
if (result[j] < result[minIndex]) {
minIndex = j;
}

}
swap(result, minIndex, i);
}

return result;
}

/**
* 交换元素
*/
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: