基數排序 (Radix sort)

來源:互聯網
上載者:User

參考: http://en.wikipedia.org/wiki/Radix_sort#Definition

計數排序,可以理解成多關鍵字排序。(因此在排序的時候需要用到穩定的排序演算法,如計數排序)

它是這樣實現的:將所有待比較數值(正整數)統一為同樣的數位長度,數位較短的數前面補零。然後,從最低位開始,依次進行一次排序。這樣從最低位排序一直到最高位排序完成以後, 數列就變成一個有序序列。

基數排序的方式可以採用LSD(Least significant digital)或MSD(Most significant digital),LSD的排序方式由索引值的最右邊開始,而MSD則相反,由索引值的最左邊開始。

An example

Original, unsorted list:

170, 45, 75, 90, 802, 24, 2, 66

Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]

17 0, 9 0, 80 2, 2, 2 4, 4 5, 7 5, 6 6   個位

Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

8 02, 2, 24, 45, 66, 1 70, 75,
9
0   十位(保證 170 在 90 前面)

Sorting by most significant digit (100s place) gives:

2, 24, 45, 66, 75, 90, 170, 802   百位
(也就是先根據個位元進行排序,再根據十位,在根據百位等等。 但是要保證上一次排序中,“相同”元素的順序。所以必須使用一個穩定的排序演算法。如計數排序)

閱讀了wiki上的代碼,加了點注釋,記錄在下:

/*基數排序轉自wiki: radix sort*/#include <stdio.h>#define MAX 5//#define SHOWPASSvoid print(int *a, int n){  int i;  for (i = 0; i < n; i++)    printf("%d\t", a[i]);} void radixsort(int *a, int n){  int i, b[MAX], m = a[0], exp = 1;  // m = a數組中最大的數字。用來控制下面的迴圈次數。  for (i = 0; i < n; i++)  {    if (a[i] > m)      m = a[i];  }   while (m / exp > 0)  {    int bucket[10] ={  0 }; //bucket清零    for (i = 0; i < n; i++)      bucket[a[i] / exp % 10]++; // a[i] / exp % 10 該運算式是求a[i]的個位元字(exp=1)、十位元字(exp=10)、百位元字(exp=100)...... // bucket[0..9] 統計各位元字的個數    for (i = 1; i < 10; i++)      bucket[i] += bucket[i - 1];// 用意是體現0~9, 權重越來越大。(更嚴格的說是儲存比它本身小的數位個數,見counting sort)    for (i = n - 1; i >= 0; i--)      b[--bucket[a[i] / exp % 10]] = a[i]; // 根據a[i]的最低位,找到它的排名    for (i = 0; i < n; i++)      a[i] = b[i];    exp *= 10;// 比較下一位, 或者說比較下一個關鍵字     #ifdef SHOWPASS      printf("\nPASS   : ");      print(a, n);    #endif  }}  int main(){  int arr[MAX];  int i, n;   printf("Enter total elements (n < %d) : ", MAX);  scanf("%d", &n);   printf("Enter %d Elements : ", n);  for (i = 0; i < n; i++)    scanf("%d", &arr[i]);    printf("\nARRAY  : ");  print(&arr[0], n);   radixsort(&arr[0], n);   printf("\nSORTED : ");  print(&arr[0], n);  printf("\n");   return 0;}/*條件宏:可以用來當做開關使用。在調試的時候開啟開關,輸出更多資訊,以便觀察程式的運行。在調試完成之後,關閉開關即可。1)在編譯的時候定義宏,使用gcc 的 -D選項。詳見man gccgcc -Wall radix_sort.c -D SHOWPASS2)或者在源檔案中直接定義宏#define SHOWPASS*/

注 : 此radix sort使用了conuting sort演算法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.