概述

在计算器科学与数学中,一个排序算法(英语:Sorting algorithm)是一种能将一串数据依照特定排序方式进行排列的一种算法。本文将总结几类常用的排序算法,包括冒泡排序、选择排序、插入排序、快速排序和归并排序。


算法原理及实现

1、冒泡排序
  • 原理图

  • 理解

通过重复地遍历要排序的列表,比较每对相邻的项目,并在顺序错误的情况下交换它们。

  • Java Code
  1. public class BubbleSort {
  2. // logic to sort the elements
  3. public static void bubble_srt(int array[]) {
  4. int n = array.length;
  5. int k;
  6. for (int m = n; m >= 0; m--) {
  7. for (int i = 0; i < n - 1; i++) {
  8. k = i + 1;
  9. if (array[i] > array[k]) {
  10. swapNumbers(i, k, array);
  11. }
  12. }
  13. printNumbers(array);
  14. }
  15. }
  16. private static void swapNumbers(int i, int j, int[] array) {
  17. int temp;
  18. temp = array[i];
  19. array[i] = array[j];
  20. array[j] = temp;
  21. }
  22. private static void printNumbers(int[] input) {
  23. for (int i = 0; i < input.length; i++) {
  24. System.out.print(input[i] + ", ");
  25. }
  26. System.out.println("\n");
  27. }
  28. public static void main(String[] args) {
  29. int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
  30. bubble_srt(input);
  31. }
  32. }

2、选择排序
  • 原理图

  • 理解

内部循环查找下一个最小(或最大)值,外部循环将该值放入其适当的位置。

  • Java Code
  1. public class SelectionSort {
  2. public static int[] doSelectionSort(int[] arr){
  3. for (int i = 0; i < arr.length - 1; i++)
  4. {
  5. int index = i;
  6. for (int j = i + 1; j < arr.length; j++)
  7. if (arr[j] < arr[index])
  8. index = j;
  9. int smallerNumber = arr[index];
  10. arr[index] = arr[i];
  11. arr[i] = smallerNumber;
  12. }
  13. return arr;
  14. }
  15. public static void main(String a[]){
  16. int[] arr1 = {10,34,2,56,7,67,88,42};
  17. int[] arr2 = doSelectionSort(arr1);
  18. for(int i:arr2){
  19. System.out.print(i);
  20. System.out.print(", ");
  21. }
  22. }
  23. }

3、插入排序
  • 原理图

  • 理解

每一步将一个待排序的记录,插入到前面已经排好序的有序序列中去,直到插完所有元素为止。

  • Java Code
  1. public class InsertionSort {
  2. public static void main(String a[]){
  3. int[] arr1 = {10,34,2,56,7,67,88,42};
  4. int[] arr2 = doInsertionSort(arr1);
  5. for(int i:arr2){
  6. System.out.print(i);
  7. System.out.print(", ");
  8. }
  9. }
  10. public static int[] doInsertionSort(int[] input){
  11. int temp;
  12. for (int i = 1; i < input.length; i++) {
  13. for(int j = i ; j > 0 ; j--){
  14. if(input[j] < input[j-1]){
  15. temp = input[j];
  16. input[j] = input[j-1];
  17. input[j-1] = temp;
  18. }
  19. }
  20. }
  21. return input;
  22. }
  23. }

4、快速排序
  • 原理图

  • 理解

将原问题分解为若干个规模更小,但结构与原问题相似的子问题,递归地解这些子问题,然后将这些子问题的解组合为原问题的解。

  • Java Code
  1. public class QuickSort {
  2. private int array[];
  3. private int length;
  4. public void sort(int[] inputArr) {
  5. if (inputArr == null || inputArr.length == 0) {
  6. return;
  7. }
  8. this.array = inputArr;
  9. length = inputArr.length;
  10. quickSort(0, length - 1);
  11. }
  12. private void quickSort(int lowerIndex, int higherIndex) {
  13. int i = lowerIndex;
  14. int j = higherIndex;
  15. // calculate pivot number, I am taking pivot as middle index number
  16. int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
  17. // Divide into two arrays
  18. while (i <= j) {
  19. /**
  20. * In each iteration, we will identify a number from left side which
  21. * is greater then the pivot value, and also we will identify a number
  22. * from right side which is less then the pivot value. Once the search
  23. * is done, then we exchange both numbers.
  24. */
  25. while (array[i] < pivot) {
  26. i++;
  27. }
  28. while (array[j] > pivot) {
  29. j--;
  30. }
  31. if (i <= j) {
  32. exchangeNumbers(i, j);
  33. //move index to next position on both sides
  34. i++;
  35. j--;
  36. }
  37. }
  38. // call quickSort() method recursively
  39. if (lowerIndex < j)
  40. quickSort(lowerIndex, j);
  41. if (i < higherIndex)
  42. quickSort(i, higherIndex);
  43. }
  44. private void exchangeNumbers(int i, int j) {
  45. int temp = array[i];
  46. array[i] = array[j];
  47. array[j] = temp;
  48. }
  49. public static void main(String a[]){
  50. MyQuickSort sorter = new MyQuickSort();
  51. int[] input = {24,2,45,20,56,75,2,56,99,53,12};
  52. sorter.sort(input);
  53. for(int i:input){
  54. System.out.print(i);
  55. System.out.print(" ");
  56. }
  57. }
  58. }

5、归并排序
  • 原理图

  • 理解

将待排序的数列分成若干个长度为1的子数列,然后将这些数列两两合并;得到若干个长度为2的有序数列,再将这些数列两两合并;得到若干个长度为4的有序数列,再将它们两两合并;直接合并成一个数列为止。

  • Java Code
  1. public class MergeSort {
  2. private int[] array;
  3. private int[] tempMergArr;
  4. private int length;
  5. public static void main(String a[]){
  6. int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
  7. MyMergeSort mms = new MyMergeSort();
  8. mms.sort(inputArr);
  9. for(int i:inputArr){
  10. System.out.print(i);
  11. System.out.print(" ");
  12. }
  13. }
  14. public void sort(int inputArr[]) {
  15. this.array = inputArr;
  16. this.length = inputArr.length;
  17. this.tempMergArr = new int[length];
  18. doMergeSort(0, length - 1);
  19. }
  20. private void doMergeSort(int lowerIndex, int higherIndex) {
  21. if (lowerIndex < higherIndex) {
  22. int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
  23. // Below step sorts the left side of the array
  24. doMergeSort(lowerIndex, middle);
  25. // Below step sorts the right side of the array
  26. doMergeSort(middle + 1, higherIndex);
  27. // Now merge both sides
  28. mergeParts(lowerIndex, middle, higherIndex);
  29. }
  30. }
  31. private void mergeParts(int lowerIndex, int middle, int higherIndex) {
  32. for (int i = lowerIndex; i <= higherIndex; i++) {
  33. tempMergArr[i] = array[i];
  34. }
  35. int i = lowerIndex;
  36. int j = middle + 1;
  37. int k = lowerIndex;
  38. while (i <= middle && j <= higherIndex) {
  39. if (tempMergArr[i] <= tempMergArr[j]) {
  40. array[k] = tempMergArr[i];
  41. i++;
  42. } else {
  43. array[k] = tempMergArr[j];
  44. j++;
  45. }
  46. k++;
  47. }
  48. while (i <= middle) {
  49. array[k] = tempMergArr[i];
  50. k++;
  51. i++;
  52. }
  53. }
  54. }

参考链接

Java Sorting Algorithms

常用排序算法及Java实现的更多相关文章

  1. 常用排序算法的Java实现 - 1

    学习编程语言时, 我们会接触到许多排序算法, 这里总结了一下常见的排序算法. 不定期更新. * 其实在Java中存在如Collections.sort()这样的方法来自动为我们排序, 不过学习排序算法 ...

  2. 常用排序算法的Java实现与分析

    由于需要分析算法的最好时间复杂度和最坏时间复杂度,因此这篇文章中写的排序都是从小到大的升序排序. 带排序的数组为arr,arr的长度为N.时间复杂度使用TC表示,额外空间复杂度使用SC表示. 好多代码 ...

  3. 数据结构与算法——常用排序算法及其Java实现

    冒泡排序 原理:依次比较相邻的两个数,将小数放在前面(左边),大数放在后面(右边),就像冒泡一样具体操作:第一趟,首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数放前 ...

  4. 插入排序,选择排序,冒泡排序等常用排序算法(java实现)

    package org.webdriver.autotest.Study; import java.util.*; public class sort_examp{ public static voi ...

  5. 几大排序算法的Java实现

    很多的面试题都问到了排序算法,中间的算法和思想比较重要,这边我选择了5种常用排序算法并用Java进行了实现.自己写一个模板已防以后面试用到.大家可以看过算法之后,自己去实现一下. 1.冒泡排序:大数向 ...

  6. Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...

  7. Java 常用排序算法/程序员必须掌握的 8大排序算法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...

  8. 我们一起来排序——使用Java语言优雅地实现常用排序算法

    破阵子·春景 燕子来时新社,梨花落后清明. 池上碧苔三四点,叶底黄鹂一两声.日长飞絮轻. 巧笑同桌伙伴,上学径里逢迎. 疑怪昨宵春梦好,元是今朝Offer拿.笑从双脸生. 排序算法--最基础的算法,互 ...

  9. Java常用排序算法及性能测试集合

    测试报告: Array length: 20000 bubbleSort : 573 ms bubbleSortAdvanced : 596 ms bubbleSortAdvanced2 : 583 ...

随机推荐

  1. Python学习笔记 | 关于python数据对象 hashable & unhashable 的理解

    文章目录 写在前面 hashable & unhashable mutable & immutable 实例检测 后续思考 参考文章 写在前面 Hash(哈希.散列)是一个将大体量数据 ...

  2. Android工具:Hierarchy Viewer

    Hierarchy Viewer 用途: 即可以用来优化自己的布局,也可以用来参考别人优秀的布局 打开方式: 运行工程,然后在\android-sdk-windows\tools目录下双击hierar ...

  3. Xcode8 报 ”xx“is missing from working copy 的问题解决方法

    方法一: Xcode->Preferences->Source->Enable Source Control 勾勾去掉 方法二: Xcode8更新后,编译工程一下子报出800多条类似 ...

  4. SqlServer_游标循环

    --从游标取的值 ) ) --获取待返现数据,并循环处理 DECLARE TestCursor CURSOR FOR SELECT ID,UserName FROM dbo.UserInfo Open ...

  5. 获取dataset结果集的第一行第一列字段

    DataSet fileNameDs = DbHelper.excuteSqlResultDataSet(strSql); ) { DataTable fileNameDt = fileNameDs. ...

  6. nano命令,vi ed pico sed joe emacs jed ex

    nano命令   nano是一个字符终端的文本编辑器,有点像DOS下的editor程序.它比vi/vim要简单得多,比较适合Linux初学者使用.某些Linux发行版的默认编辑器就是nano. nan ...

  7. java jar包 log4j不能输出解决方法

    今天运行一个jar包,jar包中使用了springContext进行加载bean和log4j配置,但是发现不能正常输入日志. 代码中增加 Xxx.class.getResource("/&q ...

  8. Linux的进程间通信-文件和文件锁

    前言 使用文件进行进程间通信应该是最先学会的一种IPC方式.任何编程语言中,文件IO都是很重要的知识,所以使用文件进行进程间通信就成了很自然被学会的一种手段.考虑到系统对文件本身存在缓存机制,使用文件 ...

  9. android APK反编译及代码混淆

    反编译.查看源代码,需要用到两个工具:dex2jar 和 jdgui dex2jar(google code) jdgui(google code),最新版本请见 官方 操作很简单,步骤如下: 1.将 ...

  10. Windows10的周年更新中无法关闭Cortana?这里有方法

    备受期待的Windows 10的周年更新中将会带来诸多新特性,包括更实用的开始菜单.Windows Ink.强化的Windows Defender,甚至还有一个无法关闭的语音助手Cortana. 目前 ...