ReviewForJob——桶式排序+基数排序(==多次桶式排序)
【0】README
1)本文旨在 給出?ReviewForJob——桶式排序+基數排序(==多次桶式排序) 的 代碼實現和代碼分析;
2)桶式排序基礎參見 http://blog.csdn.net/pacosonswjtu/article/details/49685749, 基數排序基礎參見 ?http://blog.csdn.net/pacosonswjtu/article/details/49687193
【1】桶式排序
1)intro:桶式排序適用于 許多輸入只是一些小的整數的case, 而這種輸入數據是 小整數的case 下 使用 快速排序就小題大做了;(小整數,比如所有數據小于10)
2)輸入數據: 輸入數據 A1, A2, A3, ........,An 必須只由小于 M 的整數組成;因為桶只有 M 個,M 個 桶存儲在數組中;如對10進制數字而言,桶取10;紙牌數字而言,桶取13;紙牌花色而言,桶取4;(你看到了,生活中,小整數本就直接或間接存在的)
3)桶式排序如何存儲數據的? value == index(把value看做其存儲在數組中的下標)(干貨——桶式排序的核心),即 array[Ai] = Ai,如 array[1] = 1,對的,你沒有看錯,數字1 就存儲在 下標為1的桶中,或以下標為1的鏈表中(鏈式存儲);如果有多個1的話,就存儲在 其 next 節點中;這樣一來,頭結點存儲在數組下標為1的 鏈表 存儲著輸入的所有數字1;
4)源碼實現如下:
#include <stdio.h> #include <malloc.h>#define ElementType int #define Error(str) printf("\n\t error: %s \n",str) struct Node; typedef struct Node *Node;struct Node {int value;Node next; };struct BucketSet; typedef struct BucketSet* BucketSet;struct BucketSet {int size;Node* array; };BucketSet initBucketSet(); void printArray(ElementType* data, int size); Node createNode(int value); void bucketsort(Node* buckets, ElementType value);// allocate for new Node with value. Node createNode(int value) {Node temp = (Node)malloc(sizeof(struct Node));if(temp==NULL){Error("failed createBucket() for out of space.");return NULL;}temp->next = NULL;temp->value = value;return temp; }// allocate the memory for the bucket and bucket ptr BucketSet initBucketSet(int size) {BucketSet bucketSet; int i;// allocate memory for BucketSet.bucketSet = (BucketSet)malloc(sizeof(struct BucketSet));if(bucketSet==NULL){Error("failed initBucketSet() for out of space.");return NULL; }bucketSet->size = size;// allocate memory for BucketSet->buckets.bucketSet->array = (Node*)malloc(size * sizeof(Node));if(!bucketSet->array){Error("failed initBucketSet() for out of space.");return NULL; } // allocate memory for every unit in BucketSet->buckets.for(i=0; i<size; i++){bucketSet->array[i] = createNode(-1);if(bucketSet->array[i]==NULL){ Error("failed initBucketSet() for out of space.");return NULL;} }return bucketSet; }// details of bucketSort for the input array data with size void bucketsort(Node* buckets, ElementType value) { Node temp = buckets[value]; // value is treated as index.while(temp->next){temp = temp->next;}temp->next = createNode(value);temp->next->value = value; } // 將桶中數據 copy 回 原來的數組中. void bucketsToArray(BucketSet bucketSet, ElementType* array) {Node* buckets = bucketSet->array;Node temp;int size = bucketSet->size;int i, j=0;for(i=0; i<size; i++){temp = buckets[i];while(temp->next){ array[j++] = temp->next->value;temp = temp->next;}} }void printArray(ElementType* data, int size) {int i;for(i = 0; i < size; i++) printf("\n\t data[%d] = %d", i, data[i]); printf("\n\n"); } #include "p189_bucket_sort.h"void main() { BucketSet bucketSet;ElementType data[] = {9, 6, 3, 2, 7, 7, 1, 4, 1, 0, 3, 9, 1, 1}; int size = 14, capacity=10;int i;bucketSet = initBucketSet(capacity);if(bucketSet==NULL){return ;} printf("\nbucketsort based on {9, 6, 3, 2, 7, 7, 1, 4, 1, 0, 3, 9, 1, 1}\n"); // 對10以內的數字進行桶排序.for(i=0; i<size; i++){bucketsort(bucketSet->array, data[i]); } bucketsToArray(bucketSet, data);printArray(data, size); }
對以上代碼的分析(Analysis):void bucketsort(Node* buckets, ElementType value) :桶排序的函數聲明 必須這樣寫,因為main 函數中通過循環傳入輸入數據就可以調用了,這也方便了 后續 基數排序的擴展,即 不要把 main函數中的循環 放入 bucketsort() 函數中去;
【2】基數排序
1)intro:基數排序等價于多次桶式排序;
2)基數排序中的桶式排序代碼和 單純的桶式排序代碼有一點不一樣:下面的代碼可以看到, 基數排序中的桶式排序方法 一開始就要 計算 輸入數據在某位上的數字,該數字作為 index 而不是整個 value 作為 index, 這和 單純的桶式排序有點區別;
// get the integer under the bit. int singleBit(int value, int bit) {int i=1;while(i++ < bit){value /= 10;}return value%10; }// details of bucketsort for input data. void bucketsort(Node* buckets, ElementType value, int bit) // 基數排序中的桶式排序代碼 { int index = singleBit(value, bit);Node temp = buckets[index]; // value is treated as direct or indirect index.while(temp->next){temp = temp->next;}temp->next = createNode(value); } // details of bucketSort for the input array data with size void bucketsort(Node* buckets, ElementType value) // 單純的桶式排序代碼 { Node temp = buckets[value]; // value is treated as index.while(temp->next){temp = temp->next;}temp->next = createNode(value);temp->next->value = value; } 3)基數排序代碼如下:// get the integer under the bit. int singleBit(int value, int bit) {int i=1;while(i++ < bit){value /= 10;}return value%10; }// details of bucketsort for input data. void bucketsort(Node* buckets, ElementType value, int bit) { int index = singleBit(value, bit);Node temp = buckets[index]; // value is treated as direct or indirect index.while(temp->next){temp = temp->next;}temp->next = createNode(value); } #include "p189_bucket_sort.h"//free the memory bucketSet->array own. void clearBuckets(BucketSet bucketSet) {int i, size = bucketSet->size;Node* array = bucketSet->array;Node temp, tempcopy;for(i=0; i<size; i++){temp = array[i]->next;while(temp){tempcopy = temp;temp = temp->next;free(tempcopy);tempcopy = NULL; }array[i]->next = NULL;} }// 基于桶式排序的基數排序. (待排序元素個數為size) void radixsort(ElementType* array, int size) {BucketSet bucketSet; int capacity=10; // 桶的設置為 10.int i, j;bucketSet = initBucketSet(capacity);if(bucketSet==NULL){return ;} for(i=1; i<=3; i++){for(j=0; j<size; j++){bucketsort(bucketSet->array, array[j], i);}bucketsToArray(bucketSet, array); // 別忘記將桶中數據copy 回數組.printArray(array, size); clearBuckets(bucketSet); // 每輪都要清理 數組式鏈表.} } void main() { ElementType array[] = {110, 245, 895, 658, 321, 852, 147, 458, 469, 159, 347, 28}; int size=12;printf("\n radix sort based on {110, 245, 895, 658, 321, 852, 147, 458, 469, 159, 347, 28}\n"); radixsort(array, size); }
總結
以上是生活随笔為你收集整理的ReviewForJob——桶式排序+基数排序(==多次桶式排序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10 月起影响你我生活的新规:网约车人车
- 下一篇: 虎贲中郎将是什么官 虎贲中郎将简单介绍