2011-03-01 212 views
4

好吧,所以我必須爲無符號整數和浮點數創建一個基數排序。我的未簽名的整數版本的工作原理應該是這樣,但我在獲得浮點值的時候遇到了一些麻煩。基本上它是通過浮點數的整數值對數組的值進行排序的,但是它不會根據十進制值進行排序。 (例如,36.65234將出現在36.02311之前,如果它首先出現在未排序的數組中) 此代碼段是我進行位操作和屏蔽的位置,我確信這是我的問題所在。基數在浮點數上的排序

/* For loop to create bin */ 
for(int i=0; i<n; i++){ 
    temp_int = (((unsigned int)(list[i]))>>bitwise)&0xff; 
    bin[temp_int] = bin[temp_int]+1; 
    } 

    /*For loop to get map */ 
    for (int i=0; i<256; i++) { 
    map[i+1] = bin[i]+count; 
    count = map[i+1]; 
    } 

    /* For loop to copy "sorted" values into other array */ 
    for (int i=0; i<n; i++) { 
    temp_int = (((unsigned int)(list[i]))>>bitwise)&0xff; 
    int buf_loc = map[temp_int]; 
    temp_arr[buf_loc] = list[i]; 
    map[temp_int] = map[temp_int]+1; 
    } 

在此先感謝!

+0

感謝herbalessence,我看到了其他一些例子中第一個鏈接你貼回答我的問題,我只是想知道能有人對這行代碼實現了什麼解釋? temp_int =(*(int *)&temp_arr [i])>> offset &0xff; 其中temp_arr包含一個浮點數組,offset是我想要偏移的位數(例如,我的分組是8位,所以偏移量從0 - > 8-> 16-> 24) – mike 2011-03-01 01:35:32

回答