Bucket sorting and bucket sorting algorithms

Sort buckets

Bucket Sort is a variant of base sorting. Instead of counting arrays, keywords are temporarily stored in different buckets. Read one:

Sorting sequence 123 234 45 111 6 128

The whole process is constantly allocated and collected. In addition, each bucket is a queue, so we have to follow the first-in-first-out principle when collecting data, and collect data from 0th buckets.

In practical applications, the bucket sorting method varies according to the bucket creation policy. Two different bucket creation methods are provided below. 1. Create a bucket using a static queue. 2. Use a two-dimensional array to simulate a bucket.

Static queue Bucket

# Include <iostream> # include <iomanip> using namespace std; // record type typedef struct Record {int key; int next;} record; // static queue typedef struct queue {// team head int head; // team end int tail;} StaticQueue; // obtain the maximum number of digits int get_max_digit (int array [], int n) {int digit, max; digit = 0; max = array [0]; for (int I = 1; I <n; I ++) {if (array [I]> max) max = array [I];} while (max) {digit ++; max/= 10;} return digit ;} /* collect and connect 10 buckets */void collect (Recor D rec [], StaticQueue queue [], int & first) {// k is the bucket number int tail, k = 0; // first find the first non-empty bucket while (queue [k]. head =-1) k ++; // use first to record the position of the first record and use first = queue [k] For the next allocation. head; tail = queue [k]. tail; while (k <9) {k ++; while (k <9 & queue [k]. head =-1) k ++; // connect the end of the last bucket with the header of the current bucket if (queue [k]. head! =-1) {rec [tail]. next = queue [k]. head; tail = queue [k]. tail ;}} rec [tail]. next =-1;}/* allocate n records to 10 buckets r: sort from the right number by the r bit (r = 1, 2 ...) first: subscript of the first Record */void distribute (Record rec [], int r, int first, StaticQueue queue []) {// initialize the queue first int I, d, cur = first; for (I = 0; I <10; I ++) queue [I]. head =-1; I = d = 1; while (I <r) {I ++; d * = 10;} int k; // allocate records to each bucket while (cur! =-1) {k = (rec [cur]. key/d) % 10; // if the bucket is empty, the current record is the first if (queue [k]. head =-1) queue [k]. head = cur; else // the bucket is not empty, the current record is connected to the end rec [queue [k]. tail]. next = cur; // modify the end of the queue. This sentence cannot be forgotten for queue [k]. tail = cur; // continue to split the next record into buckets. cur = rec [cur]. next ;}}/* rearrange first: subscript of the first Record */void arrange (Record rec [], int n, int first) {int I, j = first; record temp; for (I = 0; I <n-1; I ++) {temp = rec [j]; rec [j] = rec [I]; rec [I] = temp; rec [I]. next = j; j = temp. next; while (j <= I) j = rec [j]. next ;}/ * base sorting array: sequence to be sorted n: sequence size */void RadixSort (int array [], int n) {// encapsulate int I; Record * rec = new Record [n]; for (I = 0; I <n; I ++) {rec [I]. key = array [I]; rec [I]. next = I + 1;} rec [n-1]. next =-1;/* Create a static queue. Each queue is equivalent to a bucket in decimal number. Only 10 buckets are needed */StaticQueue queue [10]; // obtain the maximum number of digits for sorting int digit = get_max_digit (array, n); int first = 0; for (I = 1; I <= digit; I ++) {district (rec, I, first, queue); collect (rec, queue, first);} // rearrange arrange (rec, n, first ); // play back for (I = 0; I <n; I ++) array [I] = rec [I]. key; // release space delete [] rec;} // print void print (int array [], int n) {for (int I = 0; I <n; I ++) cout <setw (6) <array [I]; cout <endl;} int main () {cout <"******* bucket sorting (static Queue) ***** by David *****" <endl; int array [] = {123,234, 45,111, 6,128}; int n = sizeof (array)/sizeof (int); cout <"original sequence" <endl; print (array, n ); cout <"Bucket sorting" <endl; RadixSort (array, n); print (array, n); system ("pause"); return 0 ;}

Run

Code download: bucket sorting (static Queue)

Two-dimensional array Bucket

# Include <iostream> # include <iomanip> using namespace std; // maximum number of const int N = 12; // assign void distri (int array [], int n, int bucket [] [N + 1], int r) {int I; // initialize the bucket, bucket [I] [0] stores the number of elements in the bucket I for (I = 0; I <10; I ++) bucket [I] [0] = 0; int d; I = d = 1; while (I <r) {d * = 10; I ++;} int k; for (I = 0; I <n; I ++) {k = (array [I]/d) % 10; bucket [k] [0] ++; bucket [k] [bucket [k] [0] = array [I] ;}// collect void collect (int array [], int n, int bucket [] [N + 1]) {int I, j, index; index = 0; for (I = 0; I <10; I ++) for (j = 1; j <= bucket [I] [0]; j ++) array [index ++] = bucket [I] [j];} // obtain the maximum number of digits int get_max_digit (int array [], int n) {int digit, max; digit = 0; max = array [0]; for (int I = 1; I <n; I ++) {if (array [I]> max) max = array [I];} while (max) {digit ++; max/= 10;} return digit;} // sort void BucketSort (int array [], int n) {int digit = get_max_digit (array, n); int I; int bucket [10] [N + 1]; for (I = 1; I <= digit; I ++) {// allocate distribute (array, n, bucket, i); // collect (array, n, bucket) ;}/// print void print (int * a, int n) {for (int I = 0; I <n; I ++) cout <setw (6) <a [I]; cout <endl ;} int main () {cout <"******* bucket sorting (two-dimensional array) ***** by David ****" <endl; int array [] = {123,234, 45,111, 6,128}; int n = sizeof (array)/sizeof (int); cout <"original sequence" <endl; print (array, n ); cout <"Bucket sorting" <endl; BucketSort (array, n); print (array, n); system ("pause"); return 0 ;}

Run

Code download: bucket sorting (two-dimensional array)

Summary

The above two methods are the specific implementation of Bucket sorting, which is the process of allocation and collection. But there are a lot of details, you need to carefully read the code.

Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/37668921

If this is helpful, try again!

Column Directory: Data Structure and algorithm directory

Counting sorting is a sort of buckets.

The sorting algorithm is called a ranking ?? Order, that is, to sort the record strings according to the size of one or more keywords, increment or decrement.

? Category

? The sorting algorithms used in computer science are generally classified:

? The worst computing complexity, average and optimal performance, based on the serial port (list), size (n ). In general, good performance is O (N log n), and a bad behavior is Ω (N2 ). An ideal performance is O (n. The average sum of the sort algorithms that use only one abstract key comparison operation must be at least Ω (n log n ).

? Memory usage (and use of other computer resources)

? Stability: The records maintained by the stable Sorting Algorithm follow the relative sequence of the equal key (in other words, value. That is to say, a sorting algorithm is stable. That is to say, when there are two equal key records, R and S, and appear in the original sequence, where R is prior to S, in the sorting condition concatenation? It will also be before S.

? General Methods: insert, exchange, select, merge, etc. Exchange sort bubble sort and Quick Sort (Quick Sort ). Select sorting, including vibrating screen sorting and heap sorting ).

? When equal elements cannot be distinguished, such as integers, stability is not a problem. However, some assume that their first digit is sorted.

? (4, 1), (3, 1), (3, 7), (5, 6)

In this case, it can produce two different results. One is to maintain the relative order of the equivalent key, and the other is not:

? (3, 1), (3, 7), (4, 1), (5, 6) (maintain order)

? (3, 7), (3, 1), (4, 1), (5, 6) (the order is changed)

? The unstable sorting algorithm may change the record of the equivalent key in the relative sequence, but the stable sorting algorithm is never used. The unstable sorting algorithm, especially as a stabilizer. This is the key to manual expansion. Therefore, in other aspects, the same key is compared between two objects, and the work is determined in the original order, this is the final victory. However, remember that this order usually involves extra space.

? Alignment Algorithm list

? In this table, n is the number of records sorted, and k is the number of different keys.

? Stability

? Bubble sort (bubble sort)-O (N2)

? Cocktail sorting (cocktail sorting, bidirectional bubble sorting)-O (N2)

? Insert sort (insert sort)-O (N2)

? Bucket sorting (bucket sorting)-O (N); requires additional memory O (K)

? Counting sorting (counting sorting)-O (N + K); requires additional memory O (N + K)

? Merge sort (merge sort)-O (n logs n); Additional memory required is O (n)

? -O (N2)

Binary sorting tree (Binary Tree sorting)-O (n log n );? The extra memory required is O (n)

? Pigeon nest sorting (PIGEON cage sorting)-O (N + K); requires O (K) Additional memory

? Base sorting (base sorting)-O (N · K), requires an additional memory of O (n)

? Dwarf sorting-O (N2)

? Library sorting-O (n log n) has a high probability that (1 + ε) N requires additional memory

? Unstable

? Select sort select sort-O (N2)

? Hill sort-O (n log n) if you are using the best version

? Sort By carding-O (n log n)

? Heap sorting (heap sorting)-O (n logs n)

? Smoothsort-O (n log n)

? It is generally considered that the expected time of the fastest sorting (fast sorting)-O (log n) is known, and the worst case of O (n2), for large random number concatenation

? Introsort-O (n log n)

? Patiently sort-O (n logs n + k) at the outermost layer requires additional O (N + K) space, you also need to find the longest ascending subsequence (the longest ascending subsequence)

? Not practical sorting algorithms

? Mao's sorting-O (N × N) is infinite in the worst case.

? Stupid sorting-O (N3); recursion requires an additional memory of O (n2)

Beaded sorting-O ...... remaining full text>

Counting sorting is a sort of buckets.

The sorting algorithm is called a ranking ?? Order, that is, to sort the record strings according to the size of one or more keywords, increment or decrement.

? Category

? The sorting algorithms used in computer science are generally classified:

? The worst computing complexity, average and optimal performance, based on the serial port (list), size (n ). In general, good performance is O (N log n), and a bad behavior is Ω (N2 ). An ideal performance is O (n. The average sum of the sort algorithms that use only one abstract key comparison operation must be at least Ω (n log n ).

? Memory usage (and use of other computer resources)

? Stability: The records maintained by the stable Sorting Algorithm follow the relative sequence of the equal key (in other words, value. That is to say, a sorting algorithm is stable. That is to say, when there are two equal key records, R and S, and appear in the original sequence, where R is prior to S, in the sorting condition concatenation? It will also be before S.

? General Methods: insert, exchange, select, merge, etc. Exchange sort bubble sort and Quick Sort (Quick Sort ). Select sorting, including vibrating screen sorting and heap sorting ).

? When equal elements cannot be distinguished, such as integers, stability is not a problem. However, some assume that their first digit is sorted.

? (4, 1), (3, 1), (3, 7), (5, 6)

In this case, it can produce two different results. One is to maintain the relative order of the equivalent key, and the other is not:

? (3, 1), (3, 7), (4, 1), (5, 6) (maintain order)

? (3, 7), (3, 1), (4, 1), (5, 6) (the order is changed)

? The unstable sorting algorithm may change the record of the equivalent key in the relative sequence, but the stable sorting algorithm is never used. The unstable sorting algorithm, especially as a stabilizer. This is the key to manual expansion. Therefore, in other aspects, the same key is compared between two objects, and the work is determined in the original order, this is the final victory. However, remember that this order usually involves extra space.

? Alignment Algorithm list

? In this table, n is the number of records sorted, and k is the number of different keys.

? Stability

? Bubble sort (bubble sort)-O (N2)

? Cocktail sorting (cocktail sorting, bidirectional bubble sorting)-O (N2)

? Insert sort (insert sort)-O (N2)

? Bucket sorting (bucket sorting)-O (N); requires additional memory O (K)

? Counting sorting (counting sorting)-O (N + K); requires additional memory O (N + K)

? Merge sort (merge sort)-O (n logs n); Additional memory required is O (n)

? -O (N2)

Binary sorting tree (Binary Tree sorting)-O (n log n );? The extra memory required is O (n)

? Pigeon nest sorting (PIGEON cage sorting)-O (N + K); requires O (K) Additional memory

? Base sorting (base sorting)-O (N · K), requires an additional memory of O (n)

? Dwarf sorting-O (N2)

? Library sorting-O (n log n) has a high probability that (1 + ε) N requires additional memory

? Unstable

? Select sort select sort-O (N2)

? Hill sort-O (n log n) if you are using the best version

? Sort By carding-O (n log n)

? Heap sorting (heap sorting)-O (n logs n)

? Smoothsort-O (n log n)

? It is generally considered that the expected time of the fastest sorting (fast sorting)-O (log n) is known, and the worst case of O (n2), for large random number concatenation

? Introsort-O (n log n)

? Patiently sort-O (n logs n + k) at the outermost layer requires additional O (N + K) space, you also need to find the longest ascending subsequence (the longest ascending subsequence)

? Not practical sorting algorithms

? Mao's sorting-O (N × N) is infinite in the worst case.

? Stupid sorting-O (N3); recursion requires an additional memory of O (n2)

Beaded sorting-O ...... remaining full text>