c++ sorting by making array of indices -


i have project create scheduling program , 1 part of requires sorting. know how regular bubble sort project asks me way...

sort() — function sort floats array data[], creating array of sorted indices. sort() function not sort data, fills the array indx[] data[indx[0]], data[indx[1]], ..., data[indx[num_events - 1]] values of data[] in ascending order.

this code have right here sorts data doesn't way supposed done. needs way because not using objects , indices of different arrays need correspond. cant figure out make sort indices. appreciated.

void sort(float data[], int indx[], int len){   float temp;    //this loop declare array of indices   //as passed in empty   (int = 0; < len; i++){     indx[i] = i;   }    (int = 0; < len - 1; i++){     (int j = 0; j < len - 1; j++){        if (data[j] > data[j+1]){           temp = data[j];           data[j] = data[j+1];           data[j+1] = temp;         }        }     } } 

try instead:

void sort(float data[], int indx[], int len) {      float temp;      (int = 0; < len; i++) {         indx[i] = i;     }      (int = 0; < len - 1; i++) {         (int j = 0; j < len - 2; j++) {             if (data[indx[j]] > data[indx[j+1]]) {                 temp = indx[j];                 indx[j] = indx[j+1];                 indx[j+1] = temp;             }         }     }  } 

by way... there optimizations can to bubble sort method. remember each pass requires 1 test less, since 1 element gets stuck in definitive position. helps lot when comes performance, if have sort long lists.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -