what sort is this?

K

Kapteyn's Star

Hi all,
Can anyone identify this sort code? why is it very slow compard to
qsort()? and it can be improved?
Thanks.

int *next_ascending(int *arr, size_t narr, int val)
{
size_t ctr;
int *next= arr;
for(ctr= 0; ctr < narr; ++ctr)
if (arr[ctr] >= val && arr[ctr] < *next) next= &arr[ctr];
return next;
}

int *next_descending(int *arr, size_t narr, int val)
{
size_t ctr;
int *next= arr;
for(ctr= 0; ctr < narr; ++ctr)
if (arr[ctr] <= val && arr[ctr] > *next) next= &arr[ctr];
return next;
}

void sort(int *arr, size_t narr, char direction)
{
size_t ctr= 0;
int preva= INT_MIN, prevd= INT_MAX, *next, tmp;
switch (direction) {
case ASCENDING:
while (ctr < narr) {
next= next_ascending(&arr[ctr], narr-ctr, preva);
tmp= arr[ctr];
arr[ctr]= preva= *next;
*next= tmp;
ctr++;
}
break;
case DESCENDING:
while (ctr < narr) {
next= next_descending(&arr[ctr], narr-ctr, prevd);
tmp= arr[ctr];
arr[ctr]= prevd= *next;
*next= tmp;
ctr++;
}
}
}
 
K

Kapteyn's Star

jacob said:
DO YOUR OWN HOMEWORK!

It's not homework. Im not a student at all...just interested in
programming.

I came up with the algorithm but im certain it's already known. but im
just not able to find it on the web...only psudocode is given on
wikipaedia and i don't know which one is this method.
 
R

regis

arj said:
Looks like some kind of selection sort.

it s a selection sort where the (arr[ctr] >= val) condition
in next_ascending() seems useless because val is the minimum
selected in the previous step by sort() in descending direction.
So the condition is always true by inviariant of the algorithm.

Same thing for (arr[ctr] <= val) condition in next_desscending()
because val is maximum selected in the previous step by sort()
in ascending direction.

so the parameter val seems useless as is the updating of
variable preva in sort()...

But i may be wrong...
 
R

Ron Ford

It's not homework. Im not a student at all...just interested in
programming.

I came up with the algorithm but im certain it's already known. but im
just not able to find it on the web...only psudocode is given on
wikipaedia and i don't know which one is this method.

Jacob's a Texas ex-pat in Paris. He also shouted "you're queer if you
don't like penthouse," and "you poisoned me on the chanz a lyzee."

I can provide a selection sort from a syntax with C bindings, if that would
help.

Britain's Heide has the final word on this sort, probably coarse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top