一、几种常见的排序算法性能比較

排序算法 最好时间 平均时间 最坏时间 辅助内存 稳定性 备注
简单选择排序 O(n^2) O(n^2) O(n^2) O(1) 不稳定 n小时较好
直接插入排序 O(n) O(n^2) O(n^2) O(1) 稳定 大部分已有序的较好
冒泡排序 O(n) O(n^2) O(n^2) O(1) 稳定 n小时较好
希尔排序 O(n) O(nlogn) O(n^s), s∈(1,2) O(1) 不稳定 s是所选分组
高速排序 O(nlogn) O(nlogn) O(n^2) O(logn) 不稳定 n大时较好
堆排序 O(nlogn) O(nlogn) O(nlogn) O(1) 不稳定 n大时较好
归并排序 O(nlogn) O(nlogn) O(nlogn) O(n) 稳定 n大时较好

注:稳定——所有相等的数经过排序之后,仍能保持它们在排序之前的相对位置关系。

二、常见算法的实现(java)

1、选择排序(Selection Sort)

  选择排序的基本思想是对待排序的记录序列进行n-1遍的处理,第i遍处理是将L[i..n]中最小者与L[i]交换位置。这样,经过i遍处理之后,前i个记录的位置已经是正确的了。

  1. public class selectSort {
  2. public static void selectSort(int[] a){
  3. int i,j;
  4. for (i=0;i<a.length;i++){
  5. for (j=i+1;j<a.length;j++){
  6. if (a[j]<a[i]){
  7. int temp=a[i];
  8. a[i]=a[j];
  9. a[j]=temp;
  10. }
  11. }
  12. }
  13. }
  14. public static void main(String args[]){
  15. int[] a={5,4,9,8,7,6,0,1,3,-5};
  16. selectSort(a);
  17. for(int i=0;i<a.length;i++){
  18. System.out.print(a[i]+" ");
  19. }
  20. System.out.println();
  21. }
  22. }

2、插入排序(Insertion Sort)

  插入排序的基本思想是,经过i-1遍处理后,L[1..i-1]己排好序。第i遍处理仅将L[i]插入L[1..i-1]的适当位置,使得L[1..i]又是排好序的序列。

要达到这个目的。我们能够用顺序比較的方法。

首先比較L[i]和L[i-1],假设L[i-1]≤ L[i]。则L[1..i]已排好序,第i遍处理就结束了;否则交换L[i]与L[i-1]的位置,继续比較L[i-1]和L[i-2]。直到找到某一个位置j(1≤j≤i-1),使得L[j] ≤L[j+1]时为止。图1演示了对4个元素进行插入排序的过程,共须要(a),(b),(c)三次插入。

  1. public class insertSort {
  2. public static void insertSort(int[] a){
  3. if (a!=null){
  4. for (int i=1;i<a.length;i++){
  5. int temp=a[i],j=i;
  6. if (a[j-1]>temp){
  7. while(j>=1&&a[j-1]>temp){
  8. a[j]=a[j-1];
  9. j--;
  10. }
  11. }
  12. a[j]=temp;
  13. }
  14. }
  15. }
  16. public static void main(String args[]){
  17. int[] a={5,4,9,8,7,6,0,1,3,2};
  18. insertSort(a);
  19. for(int i=0;i<a.length;i++){
  20. System.out.print(a[i]+" ");
  21. }
  22. System.out.println();
  23. }
  24. }

3、冒泡排序(Bubble Sort)

  冒泡排序方法是最简单的排序方法。这样的方法的基本思想是,将待排序的元素看作是竖着排列的“气泡”。较小的元素比較轻。从而要往上浮。

在冒泡排序算法中我们要对这个“气泡”序列处理若干遍。所谓一遍处理,就是自底向上检查一遍这个序列,并时刻注意两个相邻的元素的顺序是否正确。假设发现两个相邻元素的顺序不正确。即“轻”的元素在以下,就交换它们的位置。显然,处理一遍之后。“最轻”的元素就浮到了最高位置。处理二遍之后,“次轻”的元素就浮到了次高位置。

在作第二遍处理时,由于最高位置上的元素已是“最轻”元素。所以不必检查。

一般地。第i遍处理时。不必检查第i高位置以上的元素,由于经过前面i-1遍的处理,它们已正确地排好序。

  1. public class bubbleSort {
  2. public static void bubbleSort(int[] a){
  3. for (int i=0;i<a.length;i++){
  4. for (int j=0;j<a.length-1;j++){
  5. if (a[j]>a[j+1]){
  6. int temp=a[j];
  7. a[j]=a[j+1];
  8. a[j+1]=temp;
  9. }
  10. }
  11. }
  12. }
  13. public static void main(String args[]) {
  14. int[] a = {5, 4, 9, 8, 7, 6, 0, 1, 3, 2};
  15. bubbleSort(a);
  16. for (int i = 0; i < a.length; i++) {
  17. System.out.print(a[i] + " ");
  18. }
  19. System.out.println();
  20. }
  21. }

4、希尔排序(Shell Sort)

  在直接插入排序算法中,每次插入一个数,使有序序列仅仅添加1个节点。而且对插入下一个数没有提供不论什么帮助。假设比較相隔较远距离(称为增量)的数,使得数移动时能跨过多个元素,则进行一次比較就可能消除多个元素交换。D.L.shell于1959年在以他名字命名的排序算法中实现了这一思想。

算法先将要排序的一组数按某个增量d分成若干组,每组中记录的下标相差d.对每组中所有元素进行排序,然后再用一个较小的增量对它进行。在每组中再进行排序。当增量减到1时,整个要排序的数被分成一组。排序完毕。

  1. public class shellSort {
  2. public static void shellSort(int[] array){
  3. int len=array.length;
  4. for(int h=len/2;h>0;h=h/2){//步长为h
  5. for (int i=h;i<len;i++){
  6. int temp=array[i];
  7. int j;
  8. for (j=i-h;j>=0;j-=h){//相隔h个常量。跳跃式移动。使得排序效率提高
  9. if (temp==array[j]){
  10. array[j+h]=array[h];
  11. }else break;
  12. }
  13. array[j+h]=temp;
  14. }
  15. }
  16. }
  17. public static void main(String args[]){
  18. int[] a={5,4,9,8,7,6,0,1,3,2};
  19. shellSort(a);
  20. for(int i=0;i<a.length;i++){
  21. System.out.print(a[i]+" ");
  22. }
  23. System.out.println();
  24. }
  25. }

5、高速排序(Quick Sort)

  高速排序是对冒泡排序的一种本质改进。

它的基本思想是通过一趟扫描后,使得排序序列的长度能大幅度地降低。

在冒泡排序中。一次扫描仅仅能确保最大数值的数移到正确位置,而待排序序列的长度可能仅仅降低1。高速排序通过一趟扫描,就能确保某个数(以它为基准点吧)的左边各数都比它小,右边各数都比它大。

然后又用相同的方法处理它左右两边的数,直到基准点的左右仅仅有一个元素为止。

  1. public class quickSort {
  2. public static void quickSort(int[] array,int low,int high) {
  3. if(low < high){
  4. int privotLoc = partition(array, low, high); //将表一分为二
  5. quickSort(array,low,privotLoc -1); //递归对低子表递归排序
  6. quickSort(array,privotLoc + 1,high); //递归对高子表递归排序
  7. }
  8. }
  9. public static int partition(int a[], int low, int high) {
  10. int privotKey = a[low]; //基准元素
  11. while(low < high){ //从表的两端交替地向中间扫描
  12. while(low < high && a[high] >= privotKey)
  13. --high; //从high 所指位置向前搜索。至多到low+1 位置。将比基准元素小的交换到低端
  14. swap(a,low,high);
  15. while(low < high && a[low] <= privotKey )
  16. ++low;
  17. swap(a,low,high);
  18. }
  19. return low;
  20. }
  21. public static void swap(int[] a, int i,int j) {
  22. int tmp = a[i];
  23. a[i] = a[j];
  24. a[j] = tmp;
  25. }
  26. public static void main(String args[]){
  27. int[] a={5,4,9,8,7,6,0,1,3,2};
  28. quickSort(a,0,a.length-1);
  29. for(int i=0;i<a.length;i++){
  30. System.out.print(a[i]+" ");
  31. }
  32. System.out.println();
  33. }
  34. }

6、堆排序(Heap Sort)

  堆排序是一种树形选择排序,在排序过程中,将A[n]看成是全然二叉树的顺序存储结构。利用全然二叉树中双亲结点和孩子结点之间的内在关系来选择最小的元素。

  1. /**
  2. * 堆排序
  3. *
  4. * 首先使用建立最大堆的算法建立好最大堆,
  5. * 然后将堆顶元素(最大值)与最后一个值交换,
  6. * 同一时候使得堆的长度减小1 ,调用保持最大堆性质的算法调整。
  7. * 使得堆顶元素成为最大值,此时最后一个元素已被排除在外
  8. */
  9. public class heapSort {
  10. private static int heapSize;//元素个数
  11. private static void maxHeapify( int[] array , int index ){
  12. int left = 2*index;//左孩子
  13. int right = 2*index+1;//右孩子
  14. int largest;
  15. if( left < heapSize && array[ index ] < array[ left ]){
  16. largest = left;
  17. }else{
  18. largest = index;
  19. }
  20. if( right < heapSize && array[ right ] > array[ largest ]){
  21. largest = right;
  22. }
  23. if( largest == index ){
  24. return ;
  25. } else {
  26. int temp = array[ index ];
  27. array[ index ] = array[ largest ];
  28. array[ largest ] = temp;
  29. maxHeapify( array, largest );
  30. }
  31. }
  32. /**
  33. * 建立最大堆。
  34. 在数据中,array.length/2+1一直到最后的元素都是叶子元素。
  35. * 因此从其前一个元素開始,一直到第一个元素,反复调用maxHeapify函数,使其保持最大堆的性质
  36. * @param array
  37. */
  38. private static void buildMaxHeap(int[] array){
  39. // 找出最小元素,并将其置于array[0]
  40. int min = array[0];
  41. for(int i = 1 ; i < array.length ; i++ ){
  42. if( min > array[i] ){
  43. min = array[i];
  44. array[i] = array[0];
  45. array[0] = min;
  46. }
  47. }
  48. for( int i = array.length / 2 ; i >= 1; i-- ){
  49. maxHeapify( array , i );
  50. }
  51. }
  52. /**
  53. * 堆排序:
  54. */
  55. public static void heapSort( int[] array ){
  56. buildMaxHeap( array );
  57. for(int i = array.length - 1 ; i >= 2 ; i--){
  58. int temp = array[1];
  59. array[1] = array[i];
  60. array[i] = temp;
  61. heapSize--;
  62. maxHeapify( array , 1 );
  63. }
  64. }
  65. public static void main(String args[]){
  66. int[] a={5,4,9,8,7,6,0,1,3,2};
  67. heapSize = a.length;
  68. heapSort(a);
  69. for(int i=0;i<a.length;i++){
  70. System.out.print(a[i] + " ");
  71. }
  72. System.out.println();
  73. }
  74. }

7、归并排序(Merge Sort)

  设有两个有序(升序)序列存储在同一数组中相邻的位置上,最好还是设为A[l..m],A[m+1..h],将它们归并为一个有序数列,并存储在A[l..h]。

  1. public class mergeSort {
  2. public static void MergeSort(int[] array,int p,int r){
  3. if (p<r){
  4. int q=(p+r)/2;
  5. MergeSort(array,p,q);
  6. MergeSort(array,q+1,r);
  7. Merge(array,p,q,r);
  8. }
  9. }
  10. public static void Merge(int[] array,int p,int q,int r){
  11. int n1=q-p+1;
  12. int n2=r-q;
  13. int[] L=new int[n1];
  14. int[] R=new int[n2];
  15. for (int i=0;i<n1;i++){
  16. L[i]=array[p+i];
  17. }
  18. for (int i=0;i<n2;i++){
  19. R[i]=array[q+1+i];
  20. }
  21. int i,j,k=p;
  22. for (i=0,j=0;i<n1&&j<n2;k++){
  23. if (L[i]<R[j]){
  24. array[k]=L[i];
  25. i++;
  26. }else{
  27. array[k]=R[j];
  28. j++;
  29. }
  30. }
  31. if (i<n1){
  32. for (j=i;j<n1;j++,k++)
  33. array[k]=L[j];
  34. }
  35. if (j<n2){
  36. for (i=j;i<n2;i++,k++)
  37. array[k]=R[i];
  38. }
  39. }
  40. public static void main(String args[]) {
  41. int[] a = {5, 4, 9, 8, 7, 6, 0, 1, 3, 2};
  42. MergeSort(a,0,a.length-1);
  43. for (int i = 0; i < a.length; i++) {
  44. System.out.print(a[i] + " ");
  45. }
  46. System.out.println();
  47. }
  48. }

几种常见排序算法的java实现的更多相关文章

  1. 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)

    排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...

  2. 常见排序算法总结 -- java实现

    常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...

  3. 常见排序算法(附java代码)

    常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...

  4. JavaScript版几种常见排序算法

    今天发现一篇文章讲“JavaScript版几种常见排序算法”,看着不错,推荐一下原文:http://www.w3cfuns.com/blog-5456021-5404137.html 算法描述: * ...

  5. 7种基本排序算法的Java实现

    7种基本排序算法的Java实现 转自我的Github 以下为7种基本排序算法的Java实现,以及复杂度和稳定性的相关信息. 以下为代码片段,完整的代码见Sort.java 插入排序 /** * 直接插 ...

  6. 七种经典排序算法及Java实现

    排序算法稳定性表示两个值相同的元素在排序前后是否有位置变化.如果前后位置变化,则排序算法是不稳定的,否则是稳定的.稳定性的定义符合常理,两个值相同的元素无需再次交换位置,交换位置是做了一次无用功. 下 ...

  7. 常见排序算法(java实现)

    常见排序算法介绍 冒泡排序 代码: public class BubbleSort { public static void sort(int[] array) { int tValue; for ( ...

  8. Java中几种常见排序算法

    日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序等. 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数 ...

  9. Java的几种常见排序算法

    一.所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法.排序算法在很多领域得到相当地重视,尤其是在大量数据的处理方面. ...

随机推荐

  1. bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?

    echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...

  2. iTOP-4412开发板全新升级支持4G全网通模块

    开发板支持4G,GPS,CAN,485,WIFI蓝牙,重力加速度计,陀螺仪等模块. 核心板参数 尺寸:6cm*7cm 高度:连同连接器在内0.26cm CPU:Exynos4412,四核Cortex- ...

  3. 【转】用jquery编写动态的返回顶部特效

    jquery代码: function gotoTop(min_height){ //预定义返回顶部的html代码,它的css样式默认为不显示 var gotoTop_html = '<div i ...

  4. WPF动画 - Loading加载动画

    存在问题: 最近接手公司一个比较成熟的产品项目开发(WPF桌面端),其中,在登陆系统加载时,60张图片切换,实现loading闪烁加载,快有密集恐惧症了!!! 代码如下: private void L ...

  5. 简单说一下 TCP打洞和UDP打洞

    1, TCP协议通信: 现在有两台电脑A和B.在 假设A的地址为 192.168.0.100 假设B的地址为 192.168.0.102 A想给B发送一个字符串Hello,  如果A,B之间采用TCP ...

  6. vsphere中的linux虚拟机安装vmware-tools

    先在vcenter中选中虚拟机点击安装这个工具,如图 然后这台linux虚拟机的控制台操作,挂载先建立挂载目录 cd /mnt #在挂载建一个用来挂载的文件. mkdir cdrom 使用mount命 ...

  7. 03XML Schema Definition

    1. XML Schema Definition 1. XML Schema Definition XML Schema(XML Schema Definition,XSD)用于描述 XML 文档的结 ...

  8. vim 删除单个单词,cc和dd关系

    c         功能和d相同,区别在于完成删除操作后进入INSERT MODE cc       也是删除当前行,然后进入INSERT MODE 删除每行第一个字符    :%s/^.//g   ...

  9. 大项目之网上书城(五)——主页(End)

    目录 大项目之网上书城(五)--主页(End) 主要改动 1.主页(终于完成啦) 完整代码 效果图 2.head.jsp的小改动 代码 3.login.jsp ###代码 效果图 4.login.js ...

  10. 使用Java(Jedis)链接redis报java.net.ConnectException: Connection refused: connect的错误

    redis环境:centos6 java代码运行环境:windows 第一种情况:未开启redis服务. redis-server /myredis/redis.conf (写你的redis配置文件的 ...