自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

常用排序算法總結(jié)

開發(fā) 前端 算法
在計算器科學(xué)與數(shù)學(xué)中,一個排序算法(英語:Sorting algorithm)是一種能將一串?dāng)?shù)據(jù)依照特定排序方式進(jìn)行排列的一種算法。本文將總結(jié)幾類常用的排序算法,包括冒泡排序、選擇排序、插入排序、快速排序和歸并排序,分別使用Java代碼實現(xiàn),簡要使用圖例方式介紹其實現(xiàn)原理。

 概述

在計算器科學(xué)與數(shù)學(xué)中,一個排序算法(英語:Sorting algorithm)是一種能將一串?dāng)?shù)據(jù)依照特定排序方式進(jìn)行排列的一種算法。本文將總結(jié)幾類常用的排序算法,包括冒泡排序、選擇排序、插入排序、快速排序和歸并排序,分別使用Java代碼實現(xiàn),簡要使用圖例方式介紹其實現(xiàn)原理。

算法原理及實現(xiàn)

1、冒泡排序

  • 原理圖

  • 理解

通過重復(fù)地遍歷要排序的列表,比較每對相鄰的項目,并在順序錯誤的情況下交換它們。

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

2、選擇排序

  • 原理圖

 

 

  • 理解

內(nèi)部循環(huán)查找下一個最小(或最大)值,外部循環(huán)將該值放入其適當(dāng)?shù)奈恢谩?/p>

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

冒泡排序和選擇排序的區(qū)別

1、冒泡排序是比較相鄰位置的兩個數(shù),而選擇排序是按順序比較,找最大值或者最小值;

2、冒泡排序每一輪比較后,位置不對都需要換位置,選擇排序每一輪比較都只需要換一次位置;

3、冒泡排序是通過數(shù)去找位置,選擇排序是給定位置去找數(shù)。

3、插入排序

  • 原理圖

 

 

  • 理解

每一步將一個待排序的記錄,插入到前面已經(jīng)排好序的有序序列中去,直到插完所有元素為止。

  • 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、快速排序

  • 原理圖

 

 

  • 理解

將原問題分解為若干個規(guī)模更小,但結(jié)構(gòu)與原問題相似的子問題,遞歸地解這些子問題,然后將這些子問題的解組合為原問題的解。

  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.     } 

5、歸并排序

  • 原理圖

 

 

  • 理解

將待排序的數(shù)列分成若干個長度為1的子數(shù)列,然后將這些數(shù)列兩兩合并;得到若干個長度為2的有序數(shù)列,再將這些數(shù)列兩兩合并;得到若干個長度為4的有序數(shù)列,再將它們兩兩合并;直接合并成一個數(shù)列為止。

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

常見排序算法復(fù)雜度

責(zé)任編輯:華軒 來源: segmentfault
相關(guān)推薦

2015-08-26 10:13:55

排序算法總結(jié)

2011-04-20 14:07:37

冒泡排序

2011-04-20 13:56:08

選擇排序

2011-04-20 14:19:00

希爾排序

2011-04-20 15:06:44

堆排序

2011-04-20 15:20:03

快速排序

2011-04-20 14:29:07

歸并排序

2011-04-20 12:49:44

插入排序

2011-04-20 16:05:15

基數(shù)排序

2015-09-01 10:21:53

排序算法總結(jié)

2017-04-27 10:38:28

排序算法比較分析

2012-01-09 14:29:15

Java算法

2023-04-27 09:13:20

排序算法數(shù)據(jù)結(jié)構(gòu)

2009-08-26 18:14:11

C#排序算法

2015-11-12 11:05:07

java排序算法

2020-03-20 11:22:09

人工智能機(jī)器學(xué)習(xí)技術(shù)

2012-06-27 15:33:30

Java排序算法

2021-09-04 23:40:53

算法程序員前端

2022-09-24 09:03:55

前端單元測試冒泡排序

2021-11-10 09:17:18

程序員排序算法搜索算法
點贊
收藏

51CTO技術(shù)棧公眾號