内部排序比较(Java版)

2017-06-21

目录

1 三种基本排序算法
1.1 插入排序
1.2 交换排序(冒泡)
1.3 选择排序(简单)
2 比较
3 补充
3.1 快速排序
3.2 什么是桶排序
3.3 堆排序

1 三种基本排序算法


返回

1.1 插入排序

  1. public static void InsertSort(int[] arrs) {
  2. int j;
  3. int tmp;
  4. for (int i = 1; i < arrs.length; i++) {
  5. tmp = arrs[i];
  6. for (j = i - 1; j >= 0 && tmp < arrs[j]; j--) {
  7. arrs[j + 1] = arrs[j];
  8. }
  9. arrs[j + 1] = tmp;
  10. }
  11. }

1.2 交换排序(冒泡)

  1. public static void BubbleSortS(int[] arrs) {
  2. int tmp;
  3. for (int i = arrs.length - 1; i > 0; i--) {
  4. for (int j = 0; j < i; j++) {
  5. if (arrs[j] > arrs[j + 1]) {
  6. tmp = arrs[j];
  7. arrs[j] = arrs[j + 1];
  8. arrs[j + 1] = tmp;
  9. }
  10. }
  11. }
  12. }

1.3 选择排序(简单)

  1. public static void SelectSort(int[] arrs) {
  2. int minIndex;
  3. int tmp;
  4. for (int i = 0; i < arrs.length - 1; i++) {
  5. minIndex = i;
  6. for (int j = i + 1; j < arrs.length; j++) {
  7. if (arrs[minIndex] > arrs[j])
  8. minIndex = j;
  9. }
  10.  
  11. if (minIndex != i) {
  12. tmp = arrs[minIndex];
  13. arrs[minIndex] = arrs[i];
  14. arrs[i] = tmp;
  15. }
  16. }
  17. }

2 比较


返回

排序方法 复杂度 辅助空间  内外循环 一次内循环取最大数 一次循环交换次数
插入排序 O(n2) 1 i=1->length-1
     j=i-1->0
O(1)
冒泡排序 O(n2) 1 i=length-1->1
     j=0->i-1
O(n)
选择排序 O(n2)  2 i=0->length-1
     j=i+1->length-1
O(1)

内部排序(C#)

3 补充


返回

3.1 快速排序

快速排序是对冒泡排序的一种改进。

  • 时间复杂度,最坏是O(n2),一般O(nlogn),
  • 空间复杂度(递归实现),在一般情况下的空间复杂度为O(logn),在最差的情况下,若每次只完成了一个元素,那么空间复杂度为O(n)
  1. /**
  2. * 快速排序是在冒泡排序的基础上改进而来的,冒泡排序每次只能交换相邻的两个元素,而快速排序是跳跃式的交换,交换的距离很大,因此总的比较和交换次数少了很多,速度也快了不少。
  3. * 时间复杂度,最坏是O(n2),一般O(nlogn)
  4. */
  5. public class QuickSort
  6. {
  7. public static void main(String[] args)
  8. {
  9. int[] arr=new int[]{1,5,2,3,6,8,4,9,7,5};
  10. QuickSort quickSort=new QuickSort(arr);
  11. quickSort.sort();
  12. quickSort.print();
  13. }
  14.  
  15. private int[] arr;
  16. public QuickSort(int[] arr)
  17. {
  18. this.arr=arr;
  19. }
  20.  
  21. public void sort()
  22. {
  23. quickSort(arr,0,arr.length-1);
  24. }
  25.  
  26. public void quickSort(int[] arr,int begin,int end)
  27. {
  28. if(begin<end)
  29. {
  30. int i = partition(arr, begin, end);
  31. quickSort(arr,begin,i-1);
  32. quickSort(arr,i+1,end);
  33. }
  34. }
  35.  
  36. private int partition(int[] arr, int begin, int end) {
  37. int key=arr[begin];
  38. while(begin<end)
  39. {
  40. while (begin<end&&arr[end]>=key)end--;
  41. if(begin<end) {
  42. arr[begin] = arr[end];
  43. }
  44. while(begin<end&&arr[begin]<=key)begin++;
  45. if(begin<end) {
  46. arr[end] = arr[begin];
  47. }
  48. }
  49. arr[begin]=key;
  50. return begin;
  51. }
  52.  
  53. public void print()
  54. {
  55. for(int value:arr)
  56. System.out.println(value);
  57. }
  58. }

3.2 什么是桶排序

桶排序,也叫作箱排序,是一个排序算法,也是所有排序算法中最快、最简单的排序算法。其中的思想是我们首先需要知道所有待排序元素的范围,然后需要有在这个范围内的同样数量的桶,接着把元素放到对应的桶中,最后按顺序输出。

  • 时间复杂度为O(n+m)
  • 空间复杂度是O(m),其中m为桶的个数,
  1. /**
  2. * 桶排序,也叫作箱排序,是一个排序算法,也是所有排序算法中最快、最简单的排序算法。
  3. * 其中的思想是我们首先需要知道所有待排序元素的范围,然后需要有在这个范围内的同样数量的桶,接着把元素放到对应的桶中,最后按顺序输出。
  4. * 由于时间复杂度为O(n+m),m为桶容量,如果m比n大太多,则从时间上来说,性能也并不是很好。
  5. */
  6. public class BucketSort {
  7. public static void main(String[] args)
  8. {
  9. int[] needSortedArr=new int[]{9,2,3,0,3};
  10. BucketSort bucketSort=new BucketSort(10,needSortedArr);
  11. bucketSort.sort();
  12. bucketSort.print();
  13. }
  14.  
  15. private int[] buckets;
  16. private int[] array;
  17.  
  18. public BucketSort(int range,int[] array)
  19. {
  20. this.buckets=new int[range];
  21. this.array=array;
  22. }
  23.  
  24. public void sort()
  25. {
  26. if(array!=null&&array.length!=0)
  27. {
  28. for(int i=0;i<array.length;i++)
  29. {
  30. buckets[array[i]]++;
  31. }
  32. }
  33. }
  34.  
  35. public void print()
  36. {
  37. for(int i=0;i<buckets.length;i++)
  38. {
  39. for(int j=buckets[i];j>0;j--)
  40. System.out.println(i);
  41. }
  42. }
  43. }

3.3 堆排序

堆的定义

我们可以吧这个序列看成一个二叉树,可得, 1)最大堆的根节点最大,2)上层总比下层大

  • 时间复杂度是O(logn)
  1. public class HeapSort {
  2. public static void main(String[] args) {
  3. int[] array = { 8, 6, 9, 7, 5, 4, -3, -2, -1, 0, 1, 2, 3 };
  4.  
  5. System.out.println("Before heap:");
  6. printArray(array);
  7. printHeapTree(array);
  8.  
  9. System.out.println("build max heap:");
  10. buildMaxHeap(array);
  11. printArray(array);
  12. printHeapTree(array);
  13.  
  14. heapSort(array);
  15.  
  16. System.out.println("After heap sort:");
  17. printArray(array);
  18. printHeapTree(array);
  19. }
  20.  
  21. public static void heapSort(int[] array) {
  22. if (array == null || array.length <= 1) {
  23. return;
  24. }
  25.  
  26. //建大顶堆
  27. buildMaxHeap(array);
  28.  
  29. //堆排序
  30. for (int i = array.length - 1; i >= 1; i--) {
  31. exchangeElements(array, 0, i);
  32. maxHeap(array, i, 0);
  33. }
  34. }
  35.  
  36. private static void buildMaxHeap(int[] array) {
  37. if (array == null || array.length <= 1) {
  38. return;
  39. }
  40.  
  41. int half = array.length / 2-1;
  42. for (int i = half; i >= 0; i--) {
  43. maxHeap(array, array.length, i);
  44. }
  45. }
  46.  
  47. //时间复杂度O(logN)
  48. private static void maxHeap(int[] array, int heapSize, int index) {
  49. int left = index * 2 + 1;
  50. int right = index * 2 + 2;
  51.  
  52. int largest = index;
  53. if (left < heapSize && array[left] > array[index]) {
  54. largest = left;
  55. }
  56.  
  57. if (right < heapSize && array[right] > array[largest]) {
  58. largest = right;
  59. }
  60.  
  61. if (index != largest) {
  62. exchangeElements(array, index, largest);
  63. maxHeap(array, heapSize, largest);
  64. }
  65. }
  66.  
  67. public static void printArray(int[] array) {
  68. System.out.print("{");
  69. for (int i = 0; i < array.length; i++) {
  70. System.out.print(array[i]);
  71. if (i < array.length - 1) {
  72. System.out.print(", ");
  73. }
  74. }
  75. System.out.println("}");
  76. }
  77.  
  78. private static void printHeapTree(int[] array)
  79. {
  80. for(int i=1;i<array.length;i=i*2)
  81. {
  82. for(int k=i-1;k<2*(i)-1&&k<array.length;k++)
  83. {
  84. System.out.print(array[k]+" ");
  85. }
  86. System.out.println();
  87. }
  88. }
  89.  
  90. public static void exchangeElements(int[] array, int index1, int index2) {
  91. int temp = array[index1];
  92. array[index1] = array[index2];
  93. array[index2] = temp;
  94. }
  95. }

内部排序比较(Java版)的更多相关文章

  1. 排序算法Java版,以及各自的复杂度,以及由堆排序产生的top K问题

    常用的排序算法包括: 冒泡排序:每次在无序队列里将相邻两个数依次进行比较,将小数调换到前面, 逐次比较,直至将最大的数移到最后.最将剩下的N-1个数继续比较,将次大数移至倒数第二.依此规律,直至比较结 ...

  2. 常见的链表排序(Java版)

    上篇博客中讲解了九大内部排序算法,部分算法还提供了代码实现,但是那些代码实现都是基于数组进行排序的,本篇博客就以链表排序实现几种常见的排序算法,以飨读者. 快速排序的链表实现 算法思想:对于一个链表, ...

  3. 排序算法系列:选择排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)

    在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 我总结一个清晰不罗嗦版: 原理: 从数组头元素索引i开始,寻找后面最小的值(比i位 ...

  4. 常用排序算法--java版

    package com.whw.sortPractice; import java.util.Arrays; public class Sort { /** * 遍历一个数组 * @param sor ...

  5. 高速排序(Java版)

    package com.love.test; import java.util.Scanner; /** * @author huowolf *高速排序实现 *快排是十分优秀的排序算法. *核心:分治 ...

  6. 打乱式排序的Java版实现

    项目中涉及到对大批量的数据进行打乱式排序,大概原理如下: 输入源数据:1,1,2,3,3,3,4,4 输出结果:   1,2,3,4,1,3,4,3 实现代码如下,采用递归的思想: static &l ...

  7. 排序算法Java版

    选择排序: public static void selectSort(int[]a) { int minIndex=0; int temp=0; if((a==null)||(a.length==0 ...

  8. 排序小结(java版)

    一.归并排序 package org.lxh.demo08.b; class Sort { private int[] a; private int n; Sort(int n) { a=new in ...

  9. 选择排序(java版)

    public class SelectSortTest { public static void selectSort(int[] source) { for (int i = 0; i < s ...

  10. 常见排序算法总结(java版)

    一.冒泡排序 1.原理:相邻元素两两比较,大的往后放.第一次完毕,最大值在最大索引处. 即使用相邻的两个元素一次比价,依次将最大的数放到最后. 2.代码: public static void bub ...

随机推荐

  1. Swift打印Debug日志,实现Release下不打印

    OC内,我们往往做log打印时,会考虑一个Debug环境下打印,Release下控制不打印,以节约性能消耗. OC我们可以这样做: 在pch文件内,定义如下: //打印日志 #ifdef DEBUG ...

  2. Swift3 CADisplayLink简单用法

    1.定义属性 var displayLinkTimer:CADisplayLink? = nil 2.init displayLinkTimer = CADisplayLink(target: sel ...

  3. 【虚拟化系列】VMware vSphere 5.1 虚拟机管理

        在上一博文中我们安装了强大的VMware vCenter管理中心,通过VMware vSphere Client连接到VMware vCenter管理中心, vSphere 的两个核心组件是 ...

  4. Install MySQL 5.7 on Fedora 25/24, CentOS/RHEL 7.3/6.8/5.11

    MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user ...

  5. 2011最赚钱的行业和公司排行榜(verified 版本)

    最赚钱的行业和公司排行榜(verified 版本) [外资证券]:代表性公司:高盛.中金.摩根士丹利等单位第一年收入:50-80万左右(中金第一年基本工资25万,奖金35万,福利10万)五年后收入:3 ...

  6. HDU 4324 Triangle LOVE (拓扑排序)

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. React(0.13) 服务端渲染的两个函数

    1.React.renderToString 函数,  参数是组件,返回一个字符串 <!DOCTYPE html> <html> <head> <title& ...

  8. fork()和写时复制

    写时复制技术最初产生于Unix系统,用于实现一种傻瓜式的进程创建:当发出fork(  )系统调用时,内核原样复制父进程的整个地址空间并把复制的那一份分配给子进程.这种行为是非常耗时的,因为它需要: · ...

  9. java中的switch case default break

    package com.didispace; /** * Created by gmq on 2017/08/07. * * @version 1.0 * @since 2017/08/07 10:4 ...

  10. 近期对招聘Android开发者的一些思考

    公司要招聘Android开发者,故面试了大概十来个人.由于是小公司,所以来的人大多是90后,比較年轻.90后大概二十三四岁吧,从简历上看都写了一到两年的工作经验. 也由于是小公司,所以对工作经验这些没 ...