Bucket sorting)

Source: Internet
Author: User

Bucket sorting starts from a one-dimensional positive integer array and a two-dimensional integer array. The row subscript of the Two-dimensional array is from 0 to 9, the column subscript ranges from 0 to n-1, and N is the number of values to be sorted in a one-dimensional array. Each row of the Two-dimensional array is a bucket. Write a function bucketsort, which uses an integer array and the size of the array as the parameter, and perform the following operations:

A) For each value of a one-dimensional array, place the value in each row of the bucket Array Based on the number of digits. For example, put 97 in 7th rows, 3 in 3rd rows, and 100 in 0th rows. This is called "distribution process ".

B) It is convenient to loop through rows in the bucket array and copy the value back to the original array. This is called the collection process ". The new order of the preceding values in a one-dimensional array is 100, 3, and 97.

C) repeat this process for each subsequent digit (10 digits, bits, and bits. In the second sorting, 100 is placed in 0th rows, 3 is placed in 0th rows (because 3 has no 10 rows), and 97 is placed in 9th rows. After the collection process, the average values of the one-dimensional array are 100, 3, and 97. in the third sorting, 100 is placed in the first row, 3 is placed in the 0th row, and 97 is placed in the 0th row (after 3 ). After the last collection process, the original array is ordered.

Note that the size of the two-dimensional bucket array is 10 times the size of the sorted integer array. This sorting method performs better than insert sorting, but requires a lot of memory space. Insert sorting requires only one additional data element space. This makes a time-balanced example: bucket sorting uses more memory space than insert sorting, but the performance is better. This version of Bucket sorting requires that all data be copied back to the original array during each sorting. Another method is to create the second two-dimensional bucket array and exchange data between the two bucket arrays.

Input
The first line has an integer T, indicating that there are T groups of data. Each group of data has two rows: 1st acts as an integer n (n is not greater than 100), indicating that the array has n elements; 2nd rows have n integers separated by spaces, that is, the value of each element in the array.
Output
T rows in total, N integers separated by spaces in each line, that is, the sequence after the elements of each array are sorted.
Sample Input
1
3
3 1 2

Sample output
1 2 3

# Include <iostream>
# Include <cstdlib>
Using namespace STD;

// Calculate the maximum number of digits to be sorted.
Int getlargest (int A [], int N ){
Int I, Max, temp;
For (I = 1, max = 0; I <n; I ++)
If (A [I]> A [Max]) max = I;
I = 0;
Temp = A [Max];
While (temp! = 0 ){
Temp/= 10;
I ++;
}

Return I;
}

// Calculate the I-digit number (from low to high)
Int getbitnum (INT num, int I ){
While (-- I ){
Num/= 10;
}
Return num % 10;
}

// Main program for sorting buckets
Void bucketsort (int A [], int N ){
Int I, j, Max, ** bucket = new int * [10]; // I, j is used to control the distribution element.
Int B [10] = {0}, K, L, bit; // array B [10] is used to record the number of elements in each bucket
// K and l are used to control the element recycling process.
Max = getlargest (A, N );

For (I = 0; I <10; I ++)
Bucket [I] = new int [N];

For (I = 0; I <Max; I ++ ){
For (j = 0; j <n; j ++ ){
Bit = getbitnum (A [J], I + 1 );
Bucket [bit] [B [bit] = A [J];
B [bit] ++;
}
J = 0;
For (k = 0; k <10; k ++)
For (L = 0; L <B [k]; l ++ ){
A [J ++] = bucket [k] [l];
}
For (j = 0; j <10; j ++)
B [J] = 0;
}
}

Int main (){
Int t, n, * P, I;

Scanf ("% d", & T );
While (t --){
Scanf ("% d", & N );
P = new int [N];
For (I = 0; I <n; I ++)
Scanf ("% d", & P [I]);
Bucketsort (p, N );
For (I = 0; I <n; I ++)
Printf ("% d", P [I]);
Putchar ('/N ');
}

Return 0;
}

After writing, I got pku1002 and tried it. The result is slower than the qsort in C. I didn't understand it at first. Later, I think it may be that the construction of an external sorted 2-dimensional array uses dynamic allocation. It may take too much time to dynamically allocate buckets. Therefore, the static 2 array is used for storage. The procedure is as follows:

# Include <iostream>
# Include <cstdlib>
Using namespace STD;

# Define maxnum100

Int bucket [10] [maxnum];

// Calculate the maximum number of digits to be sorted.
Int getlargest (int A [], int N ){
Int I, Max, temp;
For (I = 1, max = 0; I <n; I ++)
If (A [I]> A [Max]) max = I;
I = 0;
Temp = A [Max];
While (temp! = 0 ){
Temp/= 10;
I ++;
}

Return I;
}

// Calculate the I-digit number (from low to high)
Int getbitnum (INT num, int I ){
While (-- I ){
Num/= 10;
}
Return num % 10;
}

// Main program for sorting buckets
Void bucketsort (int A [], int N ){
Int I, j, Max; // I, j is used to control the distribution process
Int B [10] = {0}, K, L, bit; // array B [10] is used to record the number of elements in each bucket
// K and l are used to control the element recycling process.
Max = getlargest (A, N );

For (I = 0; I <Max; I ++ ){
For (j = 0; j <n; j ++ ){
Bit = getbitnum (A [J], I + 1 );
Bucket [bit] [B [bit] = A [J];
B [bit] ++;
}
J = 0;
For (k = 0; k <10; k ++)
For (L = 0; L <B [k]; l ++ ){
A [J ++] = bucket [k] [l];
}
For (j = 0; j <10; j ++)
B [J] = 0;
}
}

Int main (){
Int t, n, * P, I;

Scanf ("% d", & T );
While (t --){
Scanf ("% d", & N );
P = new int [N];
For (I = 0; I <n; I ++)
Scanf ("% d", & P [I]);
Bucketsort (p, N );
For (I = 0; I <n; I ++)
Printf ("% d", P [I]);
Putchar ('/N ');
}

Return 0;
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tctony/archive/2007/11/22/1898240.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.