加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

常用排序算法总结

发布时间:2019-09-17 23:24:57 所属栏目:建站 来源:阿里云云栖社区
导读:概述 在计算器科学与数学中,一个排序算法(英语:Sorting algorithm)是一种能将一串数据依照特定排序方式进行排列的一种算法。本文将总结几类常用的排序算法,包括冒泡排序、选择排序、插入排序、快速排序和归并排序,分别使用Java代码实现,简要使用图例

3、插入排序

  • 原理图

常用排序算法总结

  • 理解

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

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

4、快速排序

  • 原理图

常用排序算法总结

  • 理解

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

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

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读