order - Quick sort an array and store the new ordering in C -


i have 2 int arrays *start , *end size might 5 millions. *start stores start node number of edge , *end stores end node number. want sort *start in ascending way , stores new ordering scheme, before sorting:

start[0] = 5 start[1] = 7 start[2] = 8 start[3] = 1 start[4] = 4 

after sorting, becomes

start[0] = 1 start[1] = 4 start[2] = 5 start[3] = 7 start[4] = 8 

i want stores new ordering scheme "3 4 0 1 2". reason want store new ordering scheme need use rearrange *end. there function qsort() in c library can sorting job doesn't store ordering scheme. there other functions can both? or how write function on own? lot.

you should review data structure you're using. if use:

struct data {     int start;     int end; }; 

you can allocate single array of structure, , use standard qsort() function once sort (once). don't need sort order array order end array since start , end values moved in parallel because they're members of same structure:

int data_cmp(const void *vp1, const void *vp2) {      const struct data *dp1 = vp1;     const struct data *dp2 = vp2;     if (dp1->start < dp2->start)         return -1;     else if (dp1->start > dp2->start)         return +1;     else         return 0; } 

and then, in other function:

size_t nitems = 5000000; struct data *array = malloc(nitems * sizeof(*array));  // ...load array start , end point values...  qsort(array, sizeof(array[0]), nitems, data_cmp); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -