目前最经典的排序算法要属:冒泡排序,快速排序,简单插入排序,希尔排序,简单选择排序,堆排序,二路归并排序,多路鬼
并排序,计数排序,桶排序,基数排序。以下就是这些常见算法的Java实现,有兴趣的可以自行实现。现在我们就来一个个分析
介绍一下各自的基础思想和实现。讲解这些算法目的在于后续更好的分析理解Android和Java。因此我们必须掌握这些基础算发思
想。
import java.util.Arrays;
import java.util.ArrayList;
/*
*/
class Sort {
/*冒泡排序
每次比较相邻两位的数,如果前者大于后者,就交换两位的数字。
每次循环目的是将最大的数按序排到最后,就好比水中的泡泡从水底到水面越来越大。
*/
public static void bubbleSort(Integer[] array) {
for (int i = 0; i < array.length - 1; i++) {//
for (int j = 0; j < array.length - i - 1; j++) {//
if (array[j] > array[j + 1]) {
int min = array[j + 1];
array[j + 1] = array[j];
array[j] = min;
}
}
}
System.out.println("bubble sort : " + Arrays.toString(array));
}
/*快速排序
快速排序是对冒泡排序的一种改进。
它的思想是通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,
然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
通常选用序列的第一个数字作为关键数据,用来进行分割序列,先从序列的尾部递减寻找第一个比关键数据小的数,再从序列的头部递增寻找第一个比关键数据大的数,然后交换两个数据。继续寻找后续的数
直到尾部和头部碰撞结束,交换
*/
public static void quickSort(int[] array, int left, int right) {
if (left >= right) return;//请各位斟酌>和>=条件的区别
int leftIn = left;
int rightIn = right;
int key = array[leftIn];
int temp = 0;
while (left != right) {
while (array[right] >= key && right > left) {//从尾部找第一个小于key的数
right--;
}
while (array[left] <= key && right > left) {//从头部找第一个大于key的数
left++;
}
if (left < right) {//交换位置
array[left] = array[left] ^ array[right];
array[right] = array[left] ^ array[right];
array[left] = array[left] ^ array[right];
}
}//循环结束left==right
array[leftIn] = array[left];
array[left] = key;
System.out.println("quick sort : " + Arrays.toString(array));
quickSort(array, leftIn, left - 1);
quickSort(array, left + 1, rightIn);
}
/*简单选择排序
每次在未排序的数中,选择最小的或者最大的,放于序列的开始或者末尾位置。
每次循环的结果将会得到最小的数在最前面。
*/
public static void simpleSelectionSort(int[] array) {
int minIndex = 0;//我们要找最小的数,需要记录最小数的索引
int temp = 0;
for (int i = 0; i < array.length - 1; i++) {
minIndex = i;//每次循环之后,数组开始端都是已经排序好的数。
for (int j = i + 1; j < array.length; j++) {//循环遍历未排序的序列,并找出最小的数
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
System.out.println("simple selection sort : " + Arrays.toString(array));
}
/*简单插入排序
我们规定首次开始前的第一位数已经排序,取出下一个数与前一位比较,如果大于则继续,取下一位数再次与前一位,前前一位比较,如果小于则将取出的数插入到对应比较数的前面。
*/
public static void simpleInsertionSort(int[] array) {
int j;
int current;
for (int i = 1; i < array.length; i++) {//
j = i - 1;//记录当前位置的前一个数
current = array[i];//保存当前的数字需要进行插入的数字
for (; current < array[j]; j--) {//插入的时候,从后往前比较,找到合适的位置插入,插入位置之后的数字顺序往后移动。
array[j + 1] = array[j];
}//循环结束之后,此时j+1为插入的位置
array[j + 1] = current;
}
System.out.println("simple insertion sort : " + Arrays.toString(array));
}
/*希尔排序
虚位待补
*/
public static void shellSort(int[] array) {
System.out.println("shell sort : " + Arrays.toString(array));
}
/*归并排序
归并排序采用的是分治法的算法思想,意思就是分而治之,将一个序列分成长度为n/2和剩下数量为n-n/2的两个序列,
并递归继续分而治之这两个序列以及获得的子序列。当不能继续再分的时候,就两两按照大小顺序再合,层层递归的返回合并有序的序列,最终得到排序好的有序序列
*/
public static void mergeSort(int[] array, int left, int right) {
int leftIn = left;
int rightIn = right;
System.out.println("merge sort begain : " + " length:" + array.length + " left:" + left + " right:" + right);
if (left >= right) return;
int[] temp = new int[right - left + 1];
int index = 0;
int middleIndex = (right + left) / 2;
int middleIndexIn = middleIndex;
if (left < right) {
mergeSort(array, left, middleIndex);
mergeSort(array, middleIndex + 1, right);
//System.out.println("merge sort middleIndex: " + middleIndex +" leftIn:" +leftIn +" rightIn: " + rightIn);
//合并两个有序序列的方法思想就是:比较两个有序序列,总有某个序列中的一个数大于或另个序列中的最大数,
//我们就先按顺序存取两序列中小于最大数前面的数,再存剩下的数
while (leftIn <= middleIndex && middleIndexIn + 1 <= right) {
if (array[leftIn] <= array[middleIndexIn + 1]) {
temp[index++] = array[leftIn++];
} else {
temp[index++] = array[middleIndexIn++ + 1];
}
}
while (leftIn <= middleIndex) {
temp[index++] = array[leftIn++];
}
while (middleIndexIn + 1 <= right) {
temp[index++] = array[middleIndexIn++ + 1];
}
//System.out.println("merge sort temp: " + Arrays.toString(temp));
for (int i = left, j = 0; i <= right; i++, j++) {
array[i] = temp[j];
}
//System.out.println("merge sort array: " + Arrays.toString(array));
}
System.out.println("merge sort end: " + Arrays.toString(array));
}
/*计数排序
计数排序是一个非基于比较的排序算法,是一种以空间换时间的排序算法。
我们先找出序列中最大最小的数字,以此可以获得,序列中的数字的范围在min和max之间
对序列中的每个数字出现的次数就行计数,计数结束后对计数结果进行累加,获得对应数字在min和max中的下标,然后根据下标输出有序序列
*/
public static void countSort(Integer[] array) {
System.out.println("count sort begain: " + Arrays.toString(array));
int max = array[0];
int min = array[0];
int[] temp = new int[array.length];
for (int arr : array) {//找出最大最小数
if (arr > max) {
max = arr;
} else if (arr < min) {
min = arr;
}
}
int[] countArray = new int[max + 1];//这里可以根据max和min进行优化,减少数组的内存占用
//进行计数
for (int i = 0; i < array.length; i++) {
countArray[array[i]]++;
}
//就行累加计算对应数字排序之后的最大下,此时countArray[i](其中i就是array中的数)就是排序之后的下标
for (int i = 1; i < countArray.length; i++) {
countArray[i] += countArray[i - 1];
}
//输出结果
for (int i = 0; i < array.length; i++) {
temp[--countArray[array[i]]] = array[i];
}
System.out.println("count sort end: " + Arrays.toString(temp));
}
/*桶排序
桶排序是一种改进的计数排序算法。这种优化的好处是降低计数排序的空间复杂度,随之时间复杂度也会相应的有所提高。
在计数排序中,我们每个数都是相当于一个桶,即桶的大小为1。
桶排序的桶的大小为size,意思一个桶中可以放入更多大小范围的数,我们再继续对每一个桶进行排序。
*/
@SuppressWarnings("unchecked")
public static void bucketSort(int[] array) {
System.out.println("bucket sort begain: " + Arrays.toString(array));
int bucketSize = 5;
int max_length = 50;
int max = array[0];
int min = array[0];
int[] temp = new int[array.length];
for (int arr : array) {//找出最大最小数
if (arr > max) {
max = arr;
} else if (arr < min) {
min = arr;
}
}
int bucketCount = (max - min) / bucketSize + 1;
System.out.println("bucketCount:" + bucketCount + ", max:" + max + ", min" + min);
/*int[][] buket = new int[bucketCount][max_length];
int[] bucketIndex = new int[bucketCount];
for(int i = 0; i < array.length; i++) {
buket[(array[i] - min)/bucketSize][bucketIndex[(array[i] - min)/bucketSize]++] = array[i];
}
for(int[] row: buket) {
bubbleSort(row);
for(int column: row) {
System.out.print(" " + column);
}
System.out.println("\n");
}*/
ArrayList<Integer>[] bucketList = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) {
bucketList[i] = new ArrayList<Integer>();
}
for (int i = 0; i < array.length; i++) {
bucketList[(array[i] - min) / bucketSize].add(array[i]);
}
for (ArrayList list : bucketList) {
bubbleSort((Integer[]) list.toArray(new Integer[list.size()]));
}
}
public static void main(String[] args) {
int[] list = {10, 6, 1, 13, 17, 9, 4, 23, 8, 6};
System.out.println("sort begain: " + Arrays.toString(list));
/*
bubbleSort(list);
simpleSelectionSort(list);
simpleInsertionSort(list);
quickSort(list, 0, list.length-1);
mergeSort(list, 0, list.length-1);
countSort(list);
*/
bucketSort(list);
System.out.println("sort finish: " + Arrays.toString(list));
}
}
依据每个算法的实现思想,也便于我们可以更好的记住这些基本的排序算法,我们将这些排序算法基本分为两类,一类是通过比
较进行排序,一类是不通过比较进行排序。可以看出除了计数排序,桶排序,基数排序之外其他都是通过比较来进行排序的。