How do the bit manipulations in this bit-sorting code work?

前端 未结 6 1479
时光取名叫无心
时光取名叫无心 2021-01-31 22:20

Jon Bentley in Column 1 of his book programming pearls introduces a technique for sorting a sequence of non-zero positive integers using bit vectors.

I have taken the p

6条回答
  •  孤独总比滥情好
    2021-01-31 23:27

    Quoting the excerpts from Bentleys' original article in DDJ, this is what the code does at a high level:

    /* phase 1: initialize set to empty */
    
    for (i = 0; i < n; i++)
    
        bit[i] = 0
    
    /* phase 2: insert present elements */
    
    for each i in the input file
    
        bit[i] = 1
    
    /* phase 3: write sorted output */
    
    for (i = 0; i < n; i++)
    
        if bit[i] == 1
    
            write i on the output file
    

提交回复
热议问题