Daily Question 6: bitmap vector sorting, bitmap vector sorting

Source: Internet
Author: User

Daily Question 6: bitmap vector sorting, bitmap vector sorting

Bitmap vector sorting has three major advantages: 1) fast speed, complexity is O (n); 2) memory needs less; 3) sorting time is irrelevant to input element data, but it also has three very strict restrictions: 1) integer; 2) element value within a certain range (related to machine memory) 3) There are no duplicates. Its essence is to use the subscript of the bit array to record the element value, and the value of the array indicates whether the element subscript exists, therefore, we can achieve the goal of saving memory and speeding up the speed (memory saving is aimed at intensive elements, and memory saving is not necessarily necessary if it is too sparse ).
For example, to represent a set of integers in the range of 1-5, you only need one byte (8 bits) instead of 20 bytes. However, we need to regard these eight digits as an array with eight elements. If an element is 1, it indicates that the subscript of this element is in the Integer Set, if the value is 0, it indicates that the subscript does not exist. Therefore, integers 1-5 can be expressed:
0 1 1 1 1 0 0
To scan the array and output the sorting result.
However, the current programming language (except some libraries) does not have a bit data type. Therefore, you need to find a method to convert the bit to the selected data type. For simplicity, the unsigned char type can be used to represent the bit. Each unsigned char element c has eight bits. You need to determine whether the first bit is 1, you can use this element to phase with a number B (represented by eight bits) That is 1 and 0 in other bits, and the result is 0, indicates that the bit is 0. If the value is not 0, the bit is 1. To set the value of a bit, you only need to either a or B (the principle is that you can use a pen to write it on paper ). How can we calculate the number B? The number where the first digit is 1 and the other digit is 0 is 128, and the number where the other digit is 1 can be obtained by right shift operation on 128. Preparations completed:

# Include "stdafx. h "# include <iostream> using namespace std; // to make the algorithm valid for negative numbers, use the minimum and maximum values as the input void sort (int * array, int value_count, int min_value, int max_value) {if (min_value> = max_value) return; // the size of the bit vector is not determined by the size of the input array, it is determined by the range of elements in the input array //. Therefore, max_value-min_value + 1 is used to calculate the number of bit bits required for this range, however, in order to ensure that the allocated memory is sufficient // and the minimum amount is exceeded, add 7, and divide the obtained sum by 8, the int bit_map_count = (max_value-min_value + 1 + 7)/8; unsigned char * bit_map = new unsigned char [bit_map_count] indicates the size of the bitmap // Vector Array. // bitmap vectors must be initialized to 0 memset (bit_map, 0, bit_map_count * sizeof (unsigned char); int * p = array; for (int I = 0; I <value_count; ++ I, ++ p) {// ensure that the value is not negative, because the array subscript is not negative int value = * p-min_value; // divide the element value by 8 to obtain the subscript int index = value> 3 of the byte where the bitmap vector of the element is located; // obtain the position of the element in the byte. int pos_in_byte = value % 8; // set the corresponding bit to 1 bit_map [index] = bit_map [index] | (128> pos_in_byte);} p = array; for (int I = 0; I <bit_map_count; ++ I) {unsigned char c = bit_map [I]; for (int j = 0; j <8; ++ j) {// determine whether the corresponding bit is 1 if (c & (128> j) {// restore the original value of the input element, and add it to the output array * p = (I <3) + j + min_value; + + p ;}}} int _ tmain (int argc, _ TCHAR * argv []) {int n = 10; int array [] = {0,-1,-2,-3,-4,-5,-6, -7,-8,-9}; for (int I = 0; I <n; ++ I) {cout <array [I] <'';} cout <endl; sort (array, n,-9, 98); for (int I = 0; I <n; ++ I) {cout <array [I] <'';} cout <endl ;}


The program runs correctly. The sort function can be improved by calculating the value of B first, and then expanding the cycle related to B.

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.