最全排序算法原理解析、java代码实现以及总结归纳

2023-03-10,,

算法分类

  十种常见排序算法可以分为两大类:

非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序。

线性时间非比较类排序:不通过比较来决定元素间的相对次序,它可以突破基于比较排序的时间下界,以线性时间运行,因此称为线性时间非比较类排序。

  详情如下:

            

算法评估

  排序算法的性能依赖于以下三个标准:

稳定性:如果a原本在b前面,而a=b,排序之后a仍然在b的前面,则稳定;如果a原本在b的前面,而a=b,排序之后 a 可能会出现在 b 的后面,则不稳定。

时间复杂度:对排序数据的总的操作次数。反映当n变化时,操作次数呈现什么规律。

空间复杂度:是指算法在计算机内执行时所需存储空间的度量,它也是数据规模n的函数。

  排序算法综合评估如下:

            

1、冒泡排序

  冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

  1.1 算法描述

比较相邻的元素。如果第一个比第二个大,就交换它们两个;
对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数;
针对所有的元素重复以上的步骤,除了最后一个;
重复步骤1~3,直到排序完成。

  1.2 动图演示

          

  1.3 代码实现

private static void bubbleSort(int[] arr) {
long current = System.currentTimeMillis();
int count = 0, len = arr.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { // 相邻元素两两对比
int temp = arr[j + 1]; // 元素交换
arr[j + 1] = arr[j];
arr[j] = temp;
count++;
}
}
}
long cost = System.currentTimeMillis() - current;
System.out.println("bubbleSort 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
}

2、快速排序

  快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。

  2.1 算法描述

  快速排序使用分治法来把一个串(list)分为两个子串(sub-lists)。具体算法描述如下:

从数列中挑出一个元素,称为 “基准”(pivot);
重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;
递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

  2.2 动图演示

            

  2.3 代码实现

/**
* 使用分治法来把一个串(list)分为两个子串(sub-lists),通过一趟排序将待排记录分隔成独立的两部分
* 其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
*
* @param arr
*/
private static void quickSort(int[] arr) {
long current = System.currentTimeMillis();
// 使用单链表进行排序
int count = spilt(arr.clone(), 0, arr.length - 1, 0);
long cost = System.currentTimeMillis() - current; System.out.println("使用单链表进行排序花费时间为:" + cost + " ms。");
System.out.println("排序后结果为:");
printArray(arr);
System.out.println("quickSort(使用单链表进行排序) 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。"); current = System.currentTimeMillis();
// 使用双链表进行排序
count = partition(arr.clone(), 0, arr.length - 1, 0);
cost = System.currentTimeMillis() - current;
System.out.println("使用双链表进行排序花费时间为:" + cost + " ms。");
System.out.println("排序后结果为:");
printArray(arr);
System.out.println("quickSort(使用双链表进行排序) 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
} private static int spilt(int[] arr, int low, int high, int count) {
if (low >= high) {
return count; //未发生比较,比较计数器不变
}
int len = arr.length;
int temp, i = low;
for (int j = low + 1; j < len; j++) {
if (arr[low] > arr[j]) {
temp = arr[j];
arr[j] = arr[i + 1];
arr[i + 1] = temp;
i++;
count++;
}
}
if (i > low) {
temp = arr[i];
arr[i] = arr[low];
arr[low] = temp;
count++;
}
count = spilt(arr, low, i - 1, count);
count = spilt(arr, i + 1, high, count);
return count;
} public static int partition(int a[], int low, int high, int count) {
if (low < high) {
//划分数组并获取比较元素的位置
int x = a[low], temp; //将该数组第一个元素设置为比较元素
int i = low;
int j = high;
while (i < j) {
while (i < j && a[j] >= x)
j--; //从右至左找到第一个小于比较元素的数
while (i < j && a[i] <= x)
i++; //从左至右找到第一个大于比较元素的数
/*需要注意的是,这里的j--与i++的顺序不可以调换!
*如果调换了顺序,i会走过头,以至于将后面较大的元素交换到数组开头*/ //将大数与小数交换
if (i != j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
count++;
} }
//将比较元素交换到期望位置
temp = a[i];
a[i] = a[low];
a[low] = temp;
count++;
count = partition(a, low, i - 1, count); //对比较元素左边进行排序
count = partition(a, i + 1, high, count); //对比较元素右边进行排序
}
return count;
} private static void printArray(int[] arr) {
System.out.print("\t");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

3、简单插入排序(Insertion Sort)

  插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

  3.1 算法描述

  一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

从第一个元素开始,该元素可以认为已经被排序;
取出下一个元素,在已经排序的元素序列中从后向前扫描;
如果该元素(已排序)大于新元素,将该元素移到下一位置;
重复步骤3,直到找到已排序的元素小于或者等于新元素的位置;
将新元素插入到该位置后;
重复步骤2~5。

  3.2 动图演示

            

  3.3 代码实现

private static void insertionSort(int[] arr) {
long current = System.currentTimeMillis();
int len = arr.length;
int preIndex, temp, count = 0;
for (int i = 1; i < len; i++) {
preIndex = i - 1;
temp = arr[i];
while (preIndex >= 0 && arr[preIndex] > temp) {
arr[preIndex + 1] = arr[preIndex];
count++;
preIndex--;
}
arr[preIndex + 1] = temp;
count++;
}
long cost = System.currentTimeMillis() - current; System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("insertionSort 发生了 " + count + " 次移动。");
}

  3.4 算法分析

  插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。

4、希尔排序

  希尔排序是简单插入排序的改进版。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序又叫缩小增量排序

  4.1 算法描述

  先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,具体算法描述:

选择一个增量序列t1,t2,…,tk,其中ti>tj,tk=1;
按增量序列个数k,对序列进行k 趟排序;
每趟排序,根据对应的增量ti,将待排序列分割成若干长度为m 的子序列,分别对各子表进行直接插入排序。仅增量因子为1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。

  4.2 动图演示

              

  4.3 代码实现

/**
* shell Sort using exchange method(希尔排序内部用了交换的方式)
*
* @param arr
*/
private static void shellSortUsingBubble(int[] arr) {
long current = System.currentTimeMillis();
int count = 0, len = arr.length;
//间隔变化
for (int gap = len / 2; gap > 0; gap /= 2) {
//对固定间隔的排序
for (int i = gap; i < len; i++) {
for (int j = i; j > gap - 1; j -= gap) {
if (arr[j] < arr[j - gap]) {
int temp = arr[j];
arr[j] = arr[j - gap];
arr[j - gap] = temp;
count++;
}
}
}
}
long cost = System.currentTimeMillis() - current; System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("shellSortUsingBubble 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
}
/**
* shell Sort using insert Method: Knuth序列
* h = 1
* h = 3*h + 1
* 一个很长的数组,我们要用能对这个数组采用最大的间隔。最大间隔不能超过数组长度的1/3,当h>1/3时 3h+1 就大于整个数组的长度了,所以就不适合了。
*/
public static void shellSort(int[] arr) {
long current = System.currentTimeMillis();
int temp, h = 1, count = 0;
while (h < arr.length / 3) {
h = h * 3 + 1;
} for (int gap = h; gap > 0; gap = (gap - 1) / 3) { for (int i = gap; i < arr.length; i++) {
temp = arr[i];
int preIndex = i - gap;
while (preIndex >= 0 && arr[preIndex] > temp) {
arr[preIndex + gap] = arr[preIndex];
count++;
preIndex -= gap;
}
arr[preIndex + gap] = temp;
count++;
}
}
long cost = System.currentTimeMillis() - current; System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("shellSort 发生了 " + count + " 次移动。");
}

  4.4 算法分析

  希尔排序的核心在于间隔序列的设定。既可以提前设定好间隔序列,也可以动态的定义间隔序列。动态定义间隔序列的算法是《算法(第4版)》的合著者Robert Sedgewick提出的。 

5、选择排序

  选择排序(Selection-sort)是一种简单直观的排序算法。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

  5.1 算法描述

  n个记录的直接选择排序可经过n-1趟直接选择排序得到有序结果。具体算法描述如下:

初始状态:无序区为R[1..n],有序区为空;
第i趟排序(i=1,2,3…n-1)开始时,当前有序区和无序区分别为R[1..i-1]和R(i..n)。该趟排序从当前无序区中-选出关键字最小的记录 R[k],将它与无序区的第1个记录R交换,使R[1..i]和R[i+1..n)分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区;
n-1趟结束,数组有序化了。

  5.2 动图演示

            

  5.3 代码实现

private static void selectionSort(int[] arr) {
long current = System.currentTimeMillis();
int len = arr.length;
int minIndex, temp, count = 0;
for (int i = 0; i < len - 1; i++) {
minIndex = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) { // 寻找最小的数
minIndex = j; // 将最小数的索引保存
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
count++;
}
long cost = System.currentTimeMillis() - current; System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("selectionSort 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
}

  5.4 算法分析

  表现最稳定的排序算法之一,因为无论什么数据进去都是O(n2)的时间复杂度,所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。

6、堆排序

  堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。

  6.1 算法描述

将初始待排序关键字序列(R1,R2….Rn)构建成大顶堆,此堆为初始的无序区;
将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,……Rn-1)和新的有序区(Rn),且满足R[1,2…n-1]<=R[n];
由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,……Rn-1)调整为新堆,然后再次将R[1]与无序区最后一个元素交换,得到新的无序区(R1,R2….Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。

  6.2 动图演示

            

  6.3 代码实现

private static void heapSort(int[] arr, int heapSize) {
int temp;
if (heapSize > 0) {
for (int i = heapSize; i > 0; i--) {
if (arr[(i - 1) / 2] < arr[i]) {
temp = arr[i];
arr[i] = arr[(i - 1) / 2];
arr[(i - 1) / 2] = temp;
}
}
temp = arr[0];
arr[0] = arr[heapSize];
arr[heapSize] = temp;
heapSort(arr, --heapSize);
}
}

7、归并排序

  归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。

  7.1 算法描述

把长度为n的输入序列分成两个长度为n/2的子序列;
对这两个子序列分别采用归并排序;
将两个排序好的子序列合并成一个最终的排序序列。

  7.2 动图演示

             

  7.3 代码实现

private static void mergeSort(int[] arr) {
long current = System.currentTimeMillis();
int[] temp = new int[arr.length];
divide(0, arr.length - 1, arr, temp);
long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
} private static void divide(int low, int high, int[] arr, int[] temp) {
if (high <= low) {
return;
}
int mid = (low + high) / 2;
divide(low, mid, arr, temp);
divide(mid + 1, high, arr, temp); for (int i = low; i <= high; i++) {
temp[i] = arr[i];
} int i = low, j = mid + 1;
for (int index = low; index <= high; ++index) {
// 如果低位数组用完 则 将高位数组依次复制
if (i > mid) {
arr[index] = temp[j++];
}
// 如果高位数组用完 则 将低位数组依次复制
else if (j > high) {
arr[index] = temp[i++];
}
// 如果高位数组最左侧元素 小于 低位数组最左侧元素 则 将高位数组最左侧元素复制
else if (temp[j] < temp[i]) {
arr[index] = temp[j++];
}
// 如果低位数组最左侧元素 小于或等于 高位数组最左侧元素 则 将低位数组最左侧元素复制
else {
arr[index] = temp[i++];
}
} if (low == 0 && high == arr.length - 1) {
System.out.println("排序后结果为:");
printArray(arr);
}
}

  7.4 算法分析

  归并排序是一种稳定的排序方法。和选择排序一样,归并排序的性能不受输入数据的影响,但表现比选择排序好的多,因为始终都是O(nlogn)的时间复杂度。代价是需要额外的内存空间。

8、计数排序

  计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。

  8.1 算法描述

找出待排序的数组中最大和最小的元素;
统计数组中每个值为i的元素出现的次数,存入数组C的第i项;
对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加);
反向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1。

  8.2 动图演示

            

  8.3 代码实现

private static void countingSort(int[] arr,int range) {
long current = System.currentTimeMillis(); //假设数值范围为0-100
int[] count = new int[range+1];
int len = arr.length;
for (int i = 0; i < len; i++) {
count[arr[i]]++;
}
int countLen = count.length;
int data = 0;
for (int j = 0; j < countLen; j++) {
for (int k = 0; k < count[j]; k++) {
arr[data++] = j;
}
} long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
}

  8.4 算法分析

  计数排序是一个稳定的排序算法。当输入的元素是 n 个 0到 k 之间的整数时,时间复杂度是O(n+k),空间复杂度也是O(n+k),其排序速度快于任何比较排序算法。当k不是很大并且序列比较集中时,计数排序是一个很有效的排序算法。

9、基数排序

  基数排序是按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序。最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。

  9.1 算法描述

取得数组中的最大数,并取得位数;
arr为原始数组,从最低位开始取每个位组成radix数组;
对radix进行计数排序(利用计数排序适用于小范围数的特点);

  9.2 动图演示

          

          

  9.3 代码实现

/**
* 高位优先法
*
* @param arr 待排序列,必须为自然数
*/
private static void radixSort(int[] arr) {
long current = System.currentTimeMillis();
//待排序列最大值
int max = arr[0];
int exp;//指数 //计算最大值
for (int anArr : arr) {
if (anArr > max) {
max = anArr;
}
} //从个位开始,对数组进行排序
for (exp = 1; max / exp > 0; exp *= 10) {
//存储待排元素的临时数组
int[] temp = new int[arr.length];
//分桶个数
int[] buckets = new int[10]; //将数据出现的次数存储在buckets中
for (int value : arr) {
//(value / exp) % 10 :value的最底位(个位)
buckets[(value / exp) % 10]++;
} //更改buckets[i],
for (int i = 1; i < 10; i++) {
buckets[i] += buckets[i - 1];
} //将数据存储到临时数组temp中
for (int i = arr.length - 1; i >= 0; i--) {
temp[buckets[(arr[i] / exp) % 10] - 1] = arr[i];
buckets[(arr[i] / exp) % 10]--;
} //将有序元素temp赋给arr
System.arraycopy(temp, 0, arr, 0, arr.length);
} long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
}

  9.4 算法分析

  基数排序基于分别排序,分别收集,所以是稳定的。但基数排序的性能比桶排序要略差,每一次关键字的桶分配都需要O(n)的时间复杂度,而且分配之后得到新的关键字序列又需要O(n)的时间复杂度。假如待排数据可以分为d个关键字,则基数排序的时间复杂度将是O(d*2n) ,当然d要远远小于n,因此基本上还是线性级别的。

  基数排序的空间复杂度为O(n+k),其中k为桶的数量。一般来说n>>k,因此额外空间需要大概n个左右。

10、桶排序

  桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。桶排序 (Bucket sort)的工作的原理:假设输入数据服从均匀分布,将数据分到有限数量的桶里,每个桶再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排)。

  10.1 算法描述

设置一个定量的数组当作空桶;
遍历输入数据,并且把数据一个一个放到对应的桶里去;
对每个不是空的桶进行排序;
从不是空的桶里把排好序的数据拼接起来。

  10.2 图片演示

           

  10.3 代码实现

public static void bucketSort(int[] arr) {
long current = System.currentTimeMillis();
if (arr == null || arr.length == 0) {
return;
}
int len = arr.length;
// 根据原始序列的长度,设置桶的数量。这里假设每个桶放平均放4个元素
int bucketCount = len / 4;
// 遍历原始序列,找出最大值和最小值
int min = 0, max = 0;
for (int i = 0; i < len; i++) {
if (arr[i] > max) {
max = arr[i];
} else if (arr[i] < min) {
min = arr[i];
}
}
// 每个桶的数值范围
int range = (max - min + 1) / bucketCount;
int[][] buckets = new int[bucketCount][];
// 遍历原始序列
for (int i = 0; i < len; i++) {
int val = arr[i];
// 计算当前值属于哪个桶
int bucketIndex = (int) Math.floor((val - min) / range);
// 向桶中添加元素
buckets[bucketIndex] = appendItem(buckets[bucketIndex], val);
}
// 最后合并所有的桶
int k = 0;
for (int[] b : buckets) {
if (b != null) {
for (int i = 0; i < b.length; i++) {
arr[k++] = b[i];
}
}
}
long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("排序后结果为:");
printArray(arr);
} private static int[] appendItem(int[] bucketArr, int val) {
if (bucketArr == null || bucketArr.length == 0) {
return new int[]{val};
}
// 拷贝一下原来桶的序列,并增加一位
int[] arr = Arrays.copyOf(bucketArr, bucketArr.length + 1);
// 这里使用插入排序,将新的值val插入到序列中
int i;
for (i = arr.length - 2; i >= 0 && arr[i] > val; i--) {
// 从新序列arr的倒数第二位开始向前遍历(倒数第一位是新增加的空位,还没有值)
// 如果当前序列值大于val,那么向后移位
arr[i + 1] = arr[i];
}
arr[i + 1] = val;
return arr;
}

  10.4 算法分析

  桶排序的时间复杂度为O(n + c),  其中n为待排序数据量,c = n * (logn - logm), m为桶的个数。极端情况下,当桶的个数与数据量相等时,桶排序时间复杂度为O(n)。

  看一些博客里写道桶排序是稳定排序,另一些博客则说是非稳定排序。实际上,桶排序的稳定性取决于桶内排序所使用的算法,若使用插入排序,则是稳定排序,若使用快排,则是非稳定排序。

所有排序算法实现以及测试代码如下:

 public class SortMethod {

     public static void main(String[] args) {

         int[] array = {1, 34, 45, 13, 6, 68, 47, 22, 49, 33, 7, 16, 50};
// int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// int[] array = {13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; System.out.println("排序前数组为:");
printArray(array);
System.out.println("----------------------------------交 换 排 序----------------------------------");
bubbleSort(array.clone());
shellSortUsingBubble(array.clone());
quickSort(array.clone());
System.out.println("----------------------------------插 入 排 序----------------------------------");
insertionSort(array.clone());
shellSort(array.clone());
System.out.println("----------------------------------选 择 排 序----------------------------------");
selectionSort(array.clone());
heapSort(array.clone(), array.length - 1, 0);
System.out.println("----------------------------------归 并 排 序----------------------------------");
mergeSort(array.clone());
System.out.println("----------------------------------计 数 排 序----------------------------------");
countingSort(array.clone(),100);
System.out.println("----------------------------------基 数 排 序----------------------------------");
radixSort(array.clone());
System.out.println("------------------------------------桶 排 序----------------------------------");
bucketSort(array.clone());
System.out.println("------------------------------------------------------------------------------"); } private static void bubbleSort(int[] arr) {
long current = System.currentTimeMillis();
int count = 0, len = arr.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { // 相邻元素两两对比
int temp = arr[j + 1]; // 元素交换
arr[j + 1] = arr[j];
arr[j] = temp;
count++;
}
}
}
long cost = System.currentTimeMillis() - current; // System.out.println("当前排序算法花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("bubbleSort 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
} /**
* 使用分治法来把一个串(list)分为两个子串(sub-lists),通过一趟排序将待排记录分隔成独立的两部分
* 其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
*
* @param arr
*/
private static void quickSort(int[] arr) {
long current = System.currentTimeMillis();
// 使用单链表进行排序
int count = spilt(arr.clone(), 0, arr.length - 1, 0);
long cost = System.currentTimeMillis() - current; // System.out.println("使用单链表进行排序花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("quickSort(使用单链表进行排序) 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。"); // current = System.currentTimeMillis();
// // 使用双链表进行排序
count = partition(arr.clone(), 0, arr.length - 1, 0);
// cost = System.currentTimeMillis() - current;
// System.out.println("使用双链表进行排序花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("quickSort(使用双链表进行排序) 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
} private static int spilt(int[] arr, int low, int high, int count) {
if (low >= high) {
return count; //未发生比较,比较计数器不变
}
int len = arr.length;
int temp, i = low;
for (int j = low + 1; j < len; j++) {
if (arr[low] > arr[j]) {
temp = arr[j];
arr[j] = arr[i + 1];
arr[i + 1] = temp;
i++;
count++;
}
}
if (i > low) {
temp = arr[i];
arr[i] = arr[low];
arr[low] = temp;
count++;
}
count = spilt(arr, low, i - 1, count);
count = spilt(arr, i + 1, high, count);
return count;
} public static int partition(int a[], int low, int high, int count) {
if (low < high) {
//划分数组并获取比较元素的位置
int x = a[low], temp; //将该数组第一个元素设置为比较元素
int i = low;
int j = high;
while (i < j) {
while (i < j && a[j] >= x)
j--; //从右至左找到第一个小于比较元素的数
while (i < j && a[i] <= x)
i++; //从左至右找到第一个大于比较元素的数
/*需要注意的是,这里的j--与i++的顺序不可以调换!
*如果调换了顺序,i会走过头,以至于将后面较大的元素交换到数组开头*/ //将大数与小数交换
if (i != j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
count++;
} }
//将比较元素交换到期望位置
temp = a[i];
a[i] = a[low];
a[low] = temp;
count++;
count = partition(a, low, i - 1, count); //对比较元素左边进行排序
count = partition(a, i + 1, high, count); //对比较元素右边进行排序
}
return count;
} // private static int partition(int[] arr, int low, int high, int count) {
// if (low >= high) {
// return count; //未发生比较,比较计数器不变
// }
//
// int temp;
// int i = low + 1, j = high;
// if (high == low + 1 && arr[high] < arr[low]) {
// temp = arr[low];
// arr[low] = arr[high];
// arr[high] = temp;
// count++;
// return count;
// }
// while (i < j) {
// while (arr[i] <= arr[low] && j > i) {
// i++;
// }
// while (arr[j] >= arr[low] && j > i) {
// j--;
// }
// if (i < j) {
// temp = arr[j];
// arr[j] = arr[i];
// arr[i] = temp;
// count++;
// }
// }
// if (i > low + 1) {
// temp = arr[i - 1];
// arr[i - 1] = arr[low];
// arr[low] = temp;
// count++;
// }
// count = partition(arr, low, i - 2, count);
// count = partition(arr, i, high, count);
// return count;
// } /**
* 插入排序
*
* @param arr
*/
private static void insertionSort(int[] arr) {
long current = System.currentTimeMillis();
int len = arr.length;
int preIndex, temp, count = 0;
for (int i = 1; i < len; i++) {
preIndex = i - 1;
temp = arr[i];
while (preIndex >= 0 && arr[preIndex] > temp) {
arr[preIndex + 1] = arr[preIndex];
count++;
preIndex--;
}
arr[preIndex + 1] = temp;
count++;
}
long cost = System.currentTimeMillis() - current; // System.out.println("当前排序算法花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("insertionSort 发生了 " + count + " 次移动。");
} /**
* shell Sort using exchange method(希尔排序内部用了交换的方式)
*
* @param arr
*/
private static void shellSortUsingBubble(int[] arr) {
long current = System.currentTimeMillis();
int count = 0, len = arr.length;
//间隔变化
for (int gap = len / 2; gap > 0; gap /= 2) {
//对固定间隔的排序
for (int i = gap; i < len; i++) {
for (int j = i; j > gap - 1; j -= gap) {
if (arr[j] < arr[j - gap]) {
int temp = arr[j];
arr[j] = arr[j - gap];
arr[j - gap] = temp;
count++;
}
}
}
}
long cost = System.currentTimeMillis() - current; // System.out.println("当前排序算法花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("shellSortUsingBubble 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
} /**
* shell Sort using insert Method: Knuth序列
* h = 1
* h = 3*h + 1
* 一个很长的数组,我们要用能对这个数组采用最大的间隔。最大间隔不能超过数组长度的1/3,当h>1/3时 3h+1 就大于整个数组的长度了,所以就不适合了。
*/
public static void shellSort(int[] arr) {
long current = System.currentTimeMillis();
int temp, h = 1, count = 0;
while (h < arr.length / 3) {
h = h * 3 + 1;
} for (int gap = h; gap > 0; gap = (gap - 1) / 3) { for (int i = gap; i < arr.length; i++) {
temp = arr[i];
int preIndex = i - gap;
while (preIndex >= 0 && arr[preIndex] > temp) {
arr[preIndex + gap] = arr[preIndex];
count++;
preIndex -= gap;
}
arr[preIndex + gap] = temp;
count++;
}
}
long cost = System.currentTimeMillis() - current; // System.out.println("当前排序算法花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("shellSort 发生了 " + count + " 次移动。");
} private static void selectionSort(int[] arr) {
long current = System.currentTimeMillis();
int len = arr.length;
int minIndex, temp, count = 0;
for (int i = 0; i < len - 1; i++) {
minIndex = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) { // 寻找最小的数
minIndex = j; // 将最小数的索引保存
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
count++;
}
long cost = System.currentTimeMillis() - current; // System.out.println("当前排序算法花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("selectionSort 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
} private static int heapSort(int[] arr, int heapSize, int count) {
int temp;
if (heapSize > 0) {
for (int i = heapSize; i > 0; i--) {
if (arr[(i - 1) / 2] < arr[i]) {
temp = arr[i];
arr[i] = arr[(i - 1) / 2];
arr[(i - 1) / 2] = temp;
count++;
}
}
temp = arr[0];
arr[0] = arr[heapSize];
arr[heapSize] = temp;
count++;
count = heapSort(arr, --heapSize, count);
} else {
// System.out.println("排序后结果为:");
// printArray(arr);
System.out.println("selectionSort 发生了 " + count + " 次交换, 相当于 " + count * 3 + " 次移动。");
}
return count;
} private static void mergeSort(int[] arr) {
long current = System.currentTimeMillis();
int[] temp = new int[arr.length];
divide(0, arr.length - 1, arr, temp);
long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
} private static void divide(int low, int high, int[] arr, int[] temp) {
if (high <= low) {
return;
}
int mid = (low + high) / 2;
divide(low, mid, arr, temp);
divide(mid + 1, high, arr, temp); for (int i = low; i <= high; i++) {
temp[i] = arr[i];
} int i = low, j = mid + 1;
for (int index = low; index <= high; ++index) {
// 如果低位数组用完 则 将高位数组依次复制
if (i > mid) {
arr[index] = temp[j++];
}
// 如果高位数组用完 则 将低位数组依次复制
else if (j > high) {
arr[index] = temp[i++];
}
// 如果高位数组最左侧元素 小于 低位数组最左侧元素 则 将高位数组最左侧元素复制
else if (temp[j] < temp[i]) {
arr[index] = temp[j++];
}
// 如果低位数组最左侧元素 小于或等于 高位数组最左侧元素 则 将低位数组最左侧元素复制
else {
arr[index] = temp[i++];
}
} if (low == 0 && high == arr.length - 1) {
System.out.println("排序后结果为:");
printArray(arr);
}
} private static void countingSort(int[] arr,int range) {
long current = System.currentTimeMillis(); //假设数值范围为0-100
int[] count = new int[range+1];
int len = arr.length;
for (int i = 0; i < len; i++) {
count[arr[i]]++;
}
int countLen = count.length;
int data = 0;
for (int j = 0; j < countLen; j++) {
for (int k = 0; k < count[j]; k++) {
arr[data++] = j;
}
} long cost = System.currentTimeMillis() - current;
// System.out.println("当前排序算法花费时间为:" + cost + " ms。");
// System.out.println("排序后结果为:");
printArray(arr); } /**
* 高位优先法
*
* @param arr 待排序列,必须为自然数
*/
private static void radixSort(int[] arr) {
long current = System.currentTimeMillis();
//待排序列最大值
int max = arr[0];
int exp;//指数 //计算最大值
for (int anArr : arr) {
if (anArr > max) {
max = anArr;
}
} //从个位开始,对数组进行排序
for (exp = 1; max / exp > 0; exp *= 10) {
//存储待排元素的临时数组
int[] temp = new int[arr.length];
//分桶个数
int[] buckets = new int[10]; //将数据出现的次数存储在buckets中
for (int value : arr) {
//(value / exp) % 10 :value的最底位(个位)
buckets[(value / exp) % 10]++;
} //更改buckets[i],
for (int i = 1; i < 10; i++) {
buckets[i] += buckets[i - 1];
} //将数据存储到临时数组temp中
for (int i = arr.length - 1; i >= 0; i--) {
temp[buckets[(arr[i] / exp) % 10] - 1] = arr[i];
buckets[(arr[i] / exp) % 10]--;
} //将有序元素temp赋给arr
System.arraycopy(temp, 0, arr, 0, arr.length);
} long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("排序后结果为:");
printArray(arr); } public static void bucketSort(int[] arr) {
long current = System.currentTimeMillis();
if (arr == null || arr.length == 0) {
return;
}
int len = arr.length;
// 根据原始序列的长度,设置桶的数量。这里假设每个桶放平均放4个元素
int bucketCount = len / 4;
// 遍历原始序列,找出最大值和最小值
int min = 0, max = 0;
for (int i = 0; i < len; i++) {
if (arr[i] > max) {
max = arr[i];
} else if (arr[i] < min) {
min = arr[i];
}
}
// 每个桶的数值范围
int range = (max - min + 1) / bucketCount;
int[][] buckets = new int[bucketCount][];
// 遍历原始序列
for (int i = 0; i < len; i++) {
int val = arr[i];
// 计算当前值属于哪个桶
int bucketIndex = (int) Math.floor((val - min) / range);
// 向桶中添加元素
buckets[bucketIndex] = appendItem(buckets[bucketIndex], val);
}
// 最后合并所有的桶
int k = 0;
for (int[] b : buckets) {
if (b != null) {
for (int i = 0; i < b.length; i++) {
arr[k++] = b[i];
}
}
}
long cost = System.currentTimeMillis() - current;
System.out.println("当前排序算法花费时间为:" + cost + " ms。");
System.out.println("排序后结果为:");
printArray(arr);
} private static int[] appendItem(int[] bucketArr, int val) {
if (bucketArr == null || bucketArr.length == 0) {
return new int[]{val};
}
// 拷贝一下原来桶的序列,并增加一位
int[] arr = Arrays.copyOf(bucketArr, bucketArr.length + 1);
// 这里使用插入排序,将新的值val插入到序列中
int i;
for (i = arr.length - 2; i >= 0 && arr[i] > val; i--) {
// 从新序列arr的倒数第二位开始向前遍历(倒数第一位是新增加的空位,还没有值)
// 如果当前序列值大于val,那么向后移位
arr[i + 1] = arr[i];
}
arr[i + 1] = val;
return arr;
} private static void printArray(int[] arr) {
System.out.print("\t");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
} }

最全排序算法原理解析、java代码实现以及总结归纳的相关教程结束。

《最全排序算法原理解析、java代码实现以及总结归纳.doc》

下载本文的Word格式文档,以方便收藏与打印。