C Implementing Bitmap sorting

Source: Internet
Author: User

Application Scenarios:

1 to N of the non-repeating chaotic data, the total number of chaotic sequence data is not more than n, need efficient sorting.

Sorr.c

#include <stdio.h>#include<stdlib.h>#include<string.h>#defineMask 0x1F#defineShift 5/************************** * Function Name:bitvect * description:according size to malloc memory * Params: * size --input array size,every element in array can ' t over size * return:return a fixed memory *************************/int*bitvect (intsize) {    intArr_size=0; int*pt=NULL; if((Size% (1<<shift)) = =0) {arr_size=size/(1<<shift); }    Else{arr_size= (size/(1<<shift)) +1; } printf ("malloc sizeof (int) Count:%d\n", arr_size); PT=(int*)malloc(sizeof(int)*arr_size); if(!PT) {fprintf (stderr,"can ' t malloc memory \ n");returnNULL;} Memset (PT,0, arr_size); returnpt;}/************************* * Function Name:bitfree * Description:free A memory named target * Params: * Target--a Pointer that's needed to free * return:if opertation is ok,that return 1 * ***********************/intBitfree (void*target) {    if(target!=NULL) {       Free(target); Target=NULL; printf ("Target Free ok\n"); }    Else    {        return 1; }    return 0;}/*********************** * Function Name:setbit * Description:set bit value in one element from array * Params: * *     *********************/voidSetbit (intElemint*Parr) {  intPos=elem>>shift; //printf ("pos =%d\n", POS);* (Parr+pos) |= (1<< (pos&mask));}voidClsbit (intNint*Parr) {    intPos=n>>shift; //printf ("Clsbit Pos:%d\n", POS);* (Parr+pos) &= (~ (1<< (pos&Mask))) ;}intIsone (intElemint*Parr) {    intPos=elem>>shift; intflag= * (Parr+pos) & (1<< (pos&mask)); //printf ("\tisone:%d\n", flag);    returnFlag;}voidTest () {int*parr=bitvect (6); intarr[6]={1,5,4,3,2,6}; printf ("reginal data: \ n"); intI=1;  for(; i<=6; i++) {clsbit (I,parr); if(i==6) {printf ("%d\n", arr[i-1]); }        Else{printf ("%d,", arr[i-1]); }} printf ("\ n"); I=0;  for(; i<=6; i++) {setbit (arr[i-1],parr); } I=1; printf ("sort data: \ n");  for(; i<=6; i++)    {        if(Isone (I,parr)) {if(i==6) {printf ("%d\n", i); }            Else{printf ("%d,", i); }}} printf ("Bitfree (%p):%d\n", Parr,bitfree (Parr));}intMainvoid) {test (); return 0;}

C Implementing Bitmap sorting

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.