八大排序算法总结
来源:互联网 发布:大数据工程师压力大吗 编辑:程序博客网 时间:2024/06/09 18:05
选择排序 ( Selection sort)是一种简单直观的 排序算法 。它的工作原理是每一次从待排序的 数据元素 中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法(比如序列[5, 5, 3]第一次就将第一个[5]与[3]交换,导致第一个5挪动到第二个5后面)。
public class selectSort {public selectSort() {int a[] = { 1, 54, 6, 3, 78, 34, 12, 45 };int position = 0;for (int i = 0; i < a.length; i++) {int j = i + 1;position = i;int temp = a[i];for (; j < a.length; j++) {if (a[j] < temp) {temp = a[j];position = j;}}a[position] = a[i];a[i] = temp;}for (int i = 0; i < a.length; i++)System.out.println(a[i]);}}
快速排序(Quicksort)是对 冒泡排序 的一种改进。
快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以 递归 进行,以此达到整个数据变成有序 序列 。
public class quickSort {int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };public quickSort() {quick(a);for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}public int getMiddle(int[] list, int low, int high) {int tmp = list[low]; // 数组的第一个作为中轴while (low < high) {while (low < high && list[high] >= tmp) {high--;}list[low] = list[high]; // 比中轴小的记录移到低端while (low < high && list[low] <= tmp) {low++;}list[high] = list[low]; // 比中轴大的记录移到高端}list[low] = tmp; // 中轴记录到尾return low; // 返回中轴的位置}public void _quickSort(int[] list, int low, int high) {if (low < high) {int middle = getMiddle(list, low, high); // 将list数组进行一分为二_quickSort(list, low, middle - 1); // 对低字表进行递归排序_quickSort(list, middle + 1, high); // 对高字表进行递归排序}}public void quick(int[] a2) {if (a2.length > 0) { // 查看数组是否为空_quickSort(a2, 0, a2.length - 1);}}}
希尔排序 (Shell Sort)是 插入排序 的一种。也称缩小 增量 排序,是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因DL.Shell于1959年提出而得名。
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
public class shellSort {public shellSort() {int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };double d1 = a.length;int temp = 0;while (true) {d1 = Math.ceil(d1 / 2);int d = (int) d1;for (int x = 0; x < d; x++) {for (int i = x + d; i < a.length; i += d) {int j = i - d;temp = a[i];for (; j >= 0 && temp < a[j]; j -= d) {a[j + d] = a[j];}a[j + d] = temp;}}if (d == 1) {break;}for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}}}
堆排序 (Heapsort)是指利用堆积树(堆)这种 数据结构 所设计的一种 排序算法 ,它是选择排序的一种。可以利用 数组 的特点快速定位指定索引的元素。堆分为大根堆和小根堆,是 完全二叉树 。大根堆的要求是每个节点的值都不大于其父节点的值,即 A[PARENT[i]] >= A[i]。 在数组的非降序排序中,需要使用的就是大根堆,因为根据大根堆的要求可知,最大的值一定在堆顶。
import java.util.Arrays;public class HeapSort {int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };public HeapSort() {heapSort(a);}public void heapSort(int[] a) {System.out.println("开始排序");int arrayLength = a.length;// 循环建堆for (int i = 0; i < arrayLength - 1; i++) {// 建堆buildMaxHeap(a, arrayLength - 1 - i);// 交换堆顶和最后一个元素swap(a, 0, arrayLength - 1 - i);System.out.println(Arrays.toString(a));}}private void swap(int[] data, int i, int j) {// TODO Auto-generated method stubint tmp = data[i];data[i] = data[j];data[j] = tmp;}// 对data数组从0到lastIndex建大顶堆private void buildMaxHeap(int[] data, int lastIndex) {// TODO Auto-generated method stub// 从lastIndex处节点(最后一个节点)的父节点开始for (int i = (lastIndex - 1) / 2; i >= 0; i--) {// k保存正在判断的节点int k = i;// 如果当前k节点的子节点存在while (k * 2 + 1 <= lastIndex) {// k节点的左子节点的索引int biggerIndex = 2 * k + 1;// 如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在if (biggerIndex < lastIndex) {// 若果右子节点的值较大if (data[biggerIndex] < data[biggerIndex + 1]) {// biggerIndex总是记录较大子节点的索引biggerIndex++;}}// 如果k节点的值小于其较大的子节点的值if (data[k] < data[biggerIndex]) {// 交换他们swap(data, k, biggerIndex);// 将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值k = biggerIndex;} else {break;}}//while}//for}}
冒泡排序 (Bubble Sort),是一种 计算机科学 领域的较简单的 排序算法 。 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。
public class bubbleSort {public bubbleSort() {int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };int temp = 0;for (int i = 0; i < a.length - 1; i++) {for (int j = 0; j < a.length - 1 - i; j++) {if (a[j] > a[j + 1]) {temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}}}for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}}
插入排序 有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法—— 插入排序法 ,插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序, 时间复杂度 为O(n^2)。是稳定的排序方法。插入算法把要排序的 数组 分成两部分:第一部分包含了这个数组的所有元素,但将最后一个元素除外(让数组多一个空间才有插入的位置),而第二部分就只包含这一个元素(即待插入元素)。在第一部分排序完成后,再将这个最后元素插入到已排好序的第一部分中。
插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
public class insertSort {public insertSort() {int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };int temp = 0;for (int i = 1; i < a.length; i++) {int j = i - 1;temp = a[i];for (; j >= 0 && temp < a[j]; j--) {a[j + 1] = a[j]; // 将大于temp的值整体后移一个单位}a[j + 1] = temp;}for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}}
归并排序 是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路 归并 。
归并过程为:比较a[i]和a[j]的大小,若a[i]≤a[j],则将第一个有序表中的元素a[i]复制到r[k]中,并令i和k分别加上1;否则将第二个有序表中的元素a[j]复制到r[k]中,并令j和k分别加上1,如此循环下去,直到其中一个有序表取完,然后再将另一个有序表中剩余的元素复制到r中从下标k到下标t的单元。归并排序的算法我们通常用递归实现,先把待排序区间[s,t]以中点二分,接着把左边子区间排序,再把右边子区间排序,最后把左区间和右区间用一次归并操作合并成有序的区间[s,t]。
import java.util.Arrays;public class mergingSort {int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };public mergingSort() {sort(a, 0, a.length - 1);for (int i = 0; i < a.length; i++)System.out.println(a[i]);}public void sort(int[] data, int left, int right) {// TODO Auto-generated method stubif (left < right) {// 找出中间索引int center = (left + right) / 2;// 对左边数组进行递归sort(data, left, center);// 对右边数组进行递归sort(data, center + 1, right);// 合并merge(data, left, center, right);}}public void merge(int[] data, int left, int center, int right) {// TODO Auto-generated method stubint[] tmpArr = new int[data.length];int mid = center + 1;// third记录中间数组的索引int third = left;int tmp = left;while (left <= center && mid <= right) {// 从两个数组中取出最小的放入中间数组if (data[left] <= data[mid]) {tmpArr[third++] = data[left++];} else {tmpArr[third++] = data[mid++];}}// 剩余部分依次放入中间数组while (mid <= right) {tmpArr[third++] = data[mid++];}while (left <= center) {tmpArr[third++] = data[left++];}// 将中间数组中的内容复制回原数组while (tmp <= right) {data[tmp] = tmpArr[tmp++];}System.out.println(Arrays.toString(data));}}
基数排序(radix sort)属于“分配式排序”(distribution sort),又称“桶子法”(bucket sort)或bin sort,顾名思义,它是透过键值的部份资讯,将要排序的 元素分配 至某些“桶”中,藉以达到排序的作用,基数排序法是属于稳定性的排序,其 时间复杂度 为O (nlog(r)m),其中r为所采取的基数,而m为堆数,在某些时候,基数排序法的效率高于其它的稳定性排序法。
import java.util.ArrayList;import java.util.List;public class radixSort {int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,98, 54, 101, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };public radixSort() {sort(a);for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}public void sort(int[] array) {// 首先确定排序的趟数;int max = array[0];for (int i = 1; i < array.length; i++) {if (array[i] > max) {max = array[i];}}int time = 0;// 判断位数;while (max > 0) {max /= 10;time++;} // 建立10个队列;List<ArrayList> queue = new ArrayList<ArrayList>();for (int i = 0; i < 10; i++) {ArrayList<Integer> queue1 = new ArrayList<Integer>();queue.add(queue1);} // 进行time次分配和收集;for (int i = 0; i < time; i++) {// 分配数组元素;for (intj = 0; j < array.length; j++) {// 得到数字的第time+1位数;int x = array[j] % (int) Math.pow(10, i + 1)/ (int) Math.pow(10, i);ArrayList<Integer> queue2 = queue.get(x);queue2.add(array[j]);queue.set(x, queue2);}int count = 0;// 元素计数器;// 收集队列元素;for (int k = 0; k < 10; k++) {while (queue.get(k).size() > 0) {ArrayList<Integer> queue3 = queue.get(k);array[count] = queue3.get(0);queue3.remove(0);count++;}}}// for}}
关于排序:
选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法,而冒泡排序、插入排序、归并排序和基数排序是稳定的排序算法。
八大排序算法的稳定性及复杂度总结如下:
选择排序算法准则
每种排序算法都各有优缺点。因此,在实用时需根据不同情况适当选用,甚至可以将多种方法结合起来使用。
影响排序的因素有很多,平均时间复杂度低的算法并不一定就是最优的。相反,有时平均时间复杂度高的算法可能更适合某些特殊情况。同时,选择算法时还得考虑它的可读性,以利于软件的维护。一般而言,需要考虑的因素有以下四点:
1.待排序的记录数目n的大小;
2.记录本身数据量的大小,也就是记录中除关键字外的其他信息量的大小;
3.关键字的结构及其分布情况;
4.对排序稳定性的要求。
设待排序元素的个数为n。
1)当n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序序。
快速排序:是目前基于比较的内部排序中被认为是最好的方法,当待排序的关键字是随机分布时,快速排序的平均时间最短;
堆排序:如果内存空间允许且要求稳定性的,
归并排序:它有一定数量的数据移动,所以我们可能过与插入排序组合,先获得一定长度的序列,然后再合并,在效率上将有所提高。
2)当n较大,内存空间允许,且要求稳定性,推荐归并排序
3)当n较小,可采用直接插入或直接选择排序。
直接插入排序:当元素分布有序,直接插入排序将大大减少比较次数和移动记录的次数。
直接选择排序 :元素分布有序,如果不要求稳定性,选择直接选择排序
5)一般不使用或不直接使用传统的冒泡排序。
6)基数排序
它是一种稳定的排序算法,但有一定的局限性:
1、关键字可分解。
2、记录的关键字位数较少,如果密集更好
3、如果是数字时,最好是无符号的,否则将增加相应的映射复杂度,可先将其正负分开排序。
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 八大排序算法总结
- 欢迎访问我在博客园的博客
- UI16_数据持久化(NSUserDefaults,简单对象,复杂对象归档反归档,文件管理者"删除添加文件")
- 题目:矩阵的之字型遍历
- 多线程并发问题(二)
- HDU 1203 I NEED A OFFER!
- 八大排序算法总结
- Spring Hibernate 开启事务
- 题目:空格替换
- 黑马程序员---学习笔记3:进制
- curl--curl是利用URL语法在命令行方式下工作的开源文件传输工具,
- 空指针和void*指针
- 题目:翻转二叉树
- MySQL中的replace into
- 10G个整数找中位数